home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / fileio.c < prev    next >
C/C++ Source or Header  |  1993-11-22  |  106KB  |  3,689 lines

  1. /* File IO for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "config.h"
  21. #ifdef STDC_HEADERS
  22. #include <stdlib.h>
  23. #endif
  24.  
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27.  
  28. #ifdef VMS
  29. #include "vms-pwd.h"
  30. #else
  31. #ifdef WINDOWSNT
  32. #include <fcntl.h>
  33. #else
  34. #include <pwd.h>
  35. #endif /* !WINDOWSNT */
  36. #endif
  37.  
  38. #include <ctype.h>
  39.  
  40. #ifdef VMS
  41. #include "dir.h"
  42. #include <perror.h>
  43. #include <stddef.h>
  44. #include <string.h>
  45. #endif
  46.  
  47. #include <errno.h>
  48.  
  49. #ifndef vax11c
  50. #ifndef STDC_HEADERS
  51. extern int errno;
  52. extern char *sys_errlist[];
  53. extern int sys_nerr;
  54. #endif
  55. #endif
  56.  
  57. #define err_str(a) ((a) < sys_nerr ? sys_errlist[a] : "unknown error")
  58.  
  59. #ifdef APOLLO
  60. #include <sys/time.h>
  61. #endif
  62.  
  63. #ifndef USG
  64. #ifndef VMS
  65. #ifndef BSD4_1
  66. #ifndef WINDOWSNT
  67. #define HAVE_FSYNC
  68. #endif /* !WINDOWSNT */
  69. #endif
  70. #endif
  71. #endif
  72.  
  73. #include "lisp.h"
  74. #include "intervals.h"
  75. #include "buffer.h"
  76. #include "window.h"
  77.  
  78. #ifdef WINDOWSNT
  79. #define    NOMINMAX 1
  80. #include <windows.h>
  81. #endif /* !WINDOWSNT */
  82.  
  83. #ifdef VMS
  84. #include <file.h>
  85. #include <rmsdef.h>
  86. #include <fab.h>
  87. #include <nam.h>
  88. #endif
  89.  
  90. #include "systime.h"
  91.  
  92. #ifdef HPUX
  93. #include <netio.h>
  94. #ifndef HPUX8
  95. #ifndef HPUX9
  96. #include <errnet.h>
  97. #endif
  98. #endif
  99. #endif
  100.  
  101. #ifdef STDC_HEADERS
  102. #include <io.h>
  103. #endif
  104. #ifdef WINDOWSNT
  105. #include <direct.h>
  106. #include "ntproc_p.h"
  107. #endif
  108. #include "fileio_p.h"
  109. #include "alloca_p.h"
  110. #include "insdel_p.h"
  111. #include "xdisp_p.h"
  112. #include "term_p.h"
  113. #include "dired_p.h"
  114. static int ro_fsys _P_((char *path));
  115.  
  116. #ifndef O_WRONLY
  117. #define O_WRONLY 1
  118. #endif
  119.  
  120. #ifndef min
  121. #define min(a, b) ((a) < (b) ? (a) : (b))
  122. #endif
  123. #ifndef max
  124. #define max(a, b) ((a) > (b) ? (a) : (b))
  125. #endif
  126.  
  127. /* Nonzero during writing of auto-save files */
  128. int auto_saving;
  129.  
  130. /* Set by auto_save_1 to mode of original file so Fwrite_region will create
  131.    a new file with the same mode as the original */
  132. int auto_save_mode_bits;
  133.  
  134. /* Alist of elements (REGEXP . HANDLER) for file names 
  135.    whose I/O is done with a special handler.  */
  136. Lisp_Object Vfile_name_handler_alist;
  137.  
  138. /* Nonzero means, when reading a filename in the minibuffer,
  139.  start out by inserting the default directory into the minibuffer. */
  140. int insert_default_directory;
  141.  
  142. /* On VMS, nonzero means write new files with record format stmlf.
  143.    Zero means use var format.  */
  144. int vms_stmlf_recfm;
  145.  
  146. Lisp_Object Qfile_error, Qfile_already_exists;
  147.  
  148. Lisp_Object Qfile_name_history;
  149.  
  150. _VOID_
  151. report_file_error (string, data)
  152.      char *string;
  153.      Lisp_Object data;
  154. {
  155.   Lisp_Object errstring;
  156.  
  157.   if (errno >= 0 && errno < sys_nerr)
  158.     errstring = build_string (sys_errlist[errno]);
  159.   else
  160.     errstring = build_string ("undocumented error code");
  161.  
  162.   /* System error messages are capitalized.  Downcase the initial
  163.      unless it is followed by a slash.  */
  164.   if (XSTRING (errstring)->data[1] != '/')
  165.     XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]);
  166.  
  167.   while (1)
  168.     Fsignal (Qfile_error,
  169.          Fcons (build_string (string), Fcons (errstring, data)));
  170. }
  171.  
  172. Lisp_Object
  173. close_file_unwind (fd)
  174.      Lisp_Object fd;
  175. {
  176.   close (XFASTINT (fd));
  177.   return Qt;
  178. }
  179.  
  180. Lisp_Object Qexpand_file_name;
  181. Lisp_Object Qdirectory_file_name;
  182. Lisp_Object Qfile_name_directory;
  183. Lisp_Object Qfile_name_nondirectory;
  184. Lisp_Object Qunhandled_file_name_directory;
  185. Lisp_Object Qfile_name_as_directory;
  186. Lisp_Object Qcopy_file;
  187. Lisp_Object Qmake_directory;
  188. Lisp_Object Qdelete_directory;
  189. Lisp_Object Qdelete_file;
  190. Lisp_Object Qrename_file;
  191. Lisp_Object Qadd_name_to_file;
  192. Lisp_Object Qmake_symbolic_link;
  193. Lisp_Object Qfile_exists_p;
  194. Lisp_Object Qfile_executable_p;
  195. Lisp_Object Qfile_readable_p;
  196. Lisp_Object Qfile_symlink_p;
  197. Lisp_Object Qfile_writable_p;
  198. Lisp_Object Qfile_directory_p;
  199. Lisp_Object Qfile_accessible_directory_p;
  200. Lisp_Object Qfile_modes;
  201. Lisp_Object Qset_file_modes;
  202. Lisp_Object Qfile_newer_than_file_p;
  203. Lisp_Object Qinsert_file_contents;
  204. Lisp_Object Qwrite_region;
  205. Lisp_Object Qverify_visited_file_modtime;
  206. Lisp_Object Qset_visited_file_modtime;
  207.  
  208. DEFUN ("find-file-name-handler", Ffind_file_name_handler, Sfind_file_name_handler, 1, 1, 0,
  209.   "Return FILENAME's handler function, if its syntax is handled specially.\n\
  210. Otherwise, return nil.\n\
  211. A file name is handled if one of the regular expressions in\n\
  212. `file-name-handler-alist' matches it.")
  213.   (filename)
  214.     Lisp_Object filename;
  215. {
  216.   /* This function must not munge the match data.  */
  217.   Lisp_Object chain;
  218.  
  219.   CHECK_STRING (filename, 0);
  220.  
  221.   for (chain = Vfile_name_handler_alist; XTYPE (chain) == Lisp_Cons;
  222.        chain = XCONS (chain)->cdr)
  223.     {
  224.       Lisp_Object elt;
  225.       elt = XCONS (chain)->car;
  226.       if (XTYPE (elt) == Lisp_Cons)
  227.     {
  228.       Lisp_Object string;
  229.       string = XCONS (elt)->car;
  230.       if (XTYPE (string) == Lisp_String
  231.           && fast_string_match (string, filename) >= 0)
  232.         return XCONS (elt)->cdr;
  233.     }
  234.  
  235.       QUIT;
  236.     }
  237.   return Qnil;
  238. }
  239.  
  240. DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory,
  241.   1, 1, 0,
  242.   "Return the directory component in file name NAME.\n\
  243. Return nil if NAME does not include a directory.\n\
  244. Otherwise return a directory spec.\n\
  245. Given a Unix syntax file name, returns a string ending in slash;\n\
  246. on VMS, perhaps instead a string ending in `:', `]' or `>'.")
  247.   (file)
  248.      Lisp_Object file;
  249. {
  250.   register unsigned char *beg;
  251.   register unsigned char *p;
  252.   Lisp_Object handler;
  253.  
  254.   CHECK_STRING (file, 0);
  255.  
  256.   /* If the file name has special constructs in it,
  257.      call the corresponding file handler.  */
  258.   handler = Ffind_file_name_handler (file);
  259.   if (!NILP (handler))
  260.     return call2 (handler, Qfile_name_directory, file);
  261.  
  262.   beg = XSTRING (file)->data;
  263.   p = beg + XSTRING (file)->size;
  264.  
  265.   while (p != beg && !IS_ANY_SEPARATOR(p[-1])
  266. #ifdef VMS
  267.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  268. #endif /* VMS */
  269.      ) p--;
  270.  
  271.   if (p == beg)
  272.     return Qnil;
  273.   return make_string (beg, p - beg);
  274. }
  275.  
  276. DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, Sfile_name_nondirectory,
  277.   1, 1, 0,
  278.   "Return file name NAME sans its directory.\n\
  279. For example, in a Unix-syntax file name,\n\
  280. this is everything after the last slash,\n\
  281. or the entire name if it contains no slash.")
  282.   (file)
  283.      Lisp_Object file;
  284. {
  285.   register unsigned char *beg, *p, *end;
  286.   Lisp_Object handler;
  287.  
  288.   CHECK_STRING (file, 0);
  289.  
  290.   /* If the file name has special constructs in it,
  291.      call the corresponding file handler.  */
  292.   handler = Ffind_file_name_handler (file);
  293.   if (!NILP (handler))
  294.     return call2 (handler, Qfile_name_nondirectory, file);
  295.  
  296.   beg = XSTRING (file)->data;
  297.   end = p = beg + XSTRING (file)->size;
  298.  
  299.   while (p != beg && !IS_ANY_SEPARATOR(p[-1])
  300. #ifdef VMS
  301.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  302. #endif /* VMS */
  303.      ) p--;
  304.  
  305.   return make_string (p, end - p);
  306. }
  307.  
  308. DEFUN ("unhandled-file-name-directory", Funhandled_file_name_directory, Sunhandled_file_name_directory, 1, 1, 0,
  309.   "Return a directly usable directory name somehow associated with FILENAME.\n\
  310. A `directly usable' directory name is one that may be used without the\n\
  311. intervention of any file handler.\n\
  312. If FILENAME is a directly usable file itself, return\n\
  313. (file-name-directory FILENAME).\n\
  314. The `call-process' and `start-process' functions use this function to\n\
  315. get a current directory to run processes in.")
  316.   (filename)
  317.     Lisp_Object filename;
  318. {
  319.   Lisp_Object handler;
  320.  
  321.   /* If the file name has special constructs in it,
  322.      call the corresponding file handler.  */
  323.   handler = Ffind_file_name_handler (filename);
  324.   if (!NILP (handler))
  325.     return call2 (handler, Qunhandled_file_name_directory, filename);
  326.  
  327.   return Ffile_name_directory (filename);
  328. }
  329.  
  330.  
  331. char *
  332. file_name_as_directory (out, in)
  333.      char *out, *in;
  334. {
  335.   int size = strlen (in) - 1;
  336.  
  337.   strcpy (out, in);
  338.  
  339. #ifdef VMS
  340.   /* Is it already a directory string? */
  341.   if (in[size] == ':' || in[size] == ']' || in[size] == '>')
  342.     return out;
  343.   /* Is it a VMS directory file name?  If so, hack VMS syntax.  */
  344.   else if (! index (in, '/')
  345.        && ((size > 3 && ! strcmp (&in[size - 3], ".DIR"))
  346.            || (size > 3 && ! strcmp (&in[size - 3], ".dir"))
  347.            || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4)
  348.                 || ! strncmp (&in[size - 5], ".dir", 4))
  349.            && (in[size - 1] == '.' || in[size - 1] == ';')
  350.            && in[size] == '1')))
  351.     {
  352.       register char *p, *dot;
  353.       char brack;
  354.  
  355.       /* x.dir -> [.x]
  356.      dir:x.dir --> dir:[x]
  357.      dir:[x]y.dir --> dir:[x.y] */
  358.       p = in + size;
  359.       while (p != in && *p != ':' && *p != '>' && *p != ']') p--;
  360.       if (p != in)
  361.     {
  362.       strncpy (out, in, p - in);
  363.       out[p - in] = '\0';
  364.       if (*p == ':')
  365.         {
  366.           brack = ']';
  367.           strcat (out, ":[");
  368.         }
  369.       else
  370.         {
  371.           brack = *p;
  372.           strcat (out, ".");
  373.         }
  374.       p++;
  375.     }
  376.       else
  377.     {
  378.       brack = ']';
  379.       strcpy (out, "[.");
  380.     }
  381.       dot = index (p, '.');
  382.       if (dot)
  383.     {
  384.       /* blindly remove any extension */
  385.       size = strlen (out) + (dot - p);
  386.       strncat (out, p, dot - p);
  387.     }
  388.       else
  389.     {
  390.       strcat (out, p);
  391.       size = strlen (out);
  392.     }
  393.       out[size++] = brack;
  394.       out[size] = '\0';
  395.     }
  396. #else /* not VMS */
  397.   /* Append a path separator if necessary */
  398.   if (!IS_ANY_SEPARATOR(out[size]))
  399.   {
  400.       out[size+1] = PATH_SEPARATOR;
  401.       out[size+2] = '\0';
  402.   }
  403. #endif /* not VMS */
  404.   return out;
  405. }
  406.  
  407. DEFUN ("file-name-as-directory", Ffile_name_as_directory,
  408.        Sfile_name_as_directory, 1, 1, 0,
  409.   "Return a string representing file FILENAME interpreted as a directory.\n\
  410. This operation exists because a directory is also a file, but its name as\n\
  411. a directory is different from its name as a file.\n\
  412. The result can be used as the value of `default-directory'\n\
  413. or passed as second argument to `expand-file-name'.\n\
  414. For a Unix-syntax file name, just appends a slash.\n\
  415. On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.")
  416.   (file)
  417.      Lisp_Object file;
  418. {
  419.   char *buf;
  420.   Lisp_Object handler;
  421.  
  422.   CHECK_STRING (file, 0);
  423.   if (NILP (file))
  424.     return Qnil;
  425.  
  426.   /* If the file name has special constructs in it,
  427.      call the corresponding file handler.  */
  428.   handler = Ffind_file_name_handler (file);
  429.   if (!NILP (handler))
  430.     return call2 (handler, Qfile_name_as_directory, file);
  431.  
  432.   buf = (char *) alloca (XSTRING (file)->size + 10);
  433.   return build_string (file_name_as_directory (buf, XSTRING (file)->data));
  434. }
  435.  
  436. /*
  437.  * Convert from directory name to filename.
  438.  * On VMS:
  439.  *       xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
  440.  *       xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
  441.  * On UNIX, it's simple: just make sure there is a terminating /
  442.  
  443.  * Value is nonzero if the string output is different from the input.
  444.  */
  445.  
  446. int
  447. directory_file_name (src, dst)
  448.      char *src, *dst;
  449. {
  450.   long slen;
  451. #ifdef VMS
  452.   long rlen;
  453.   char * ptr, * rptr;
  454.   char bracket;
  455.   struct FAB fab = cc$rms_fab;
  456.   struct NAM nam = cc$rms_nam;
  457.   char esa[NAM$C_MAXRSS];
  458. #endif /* VMS */
  459.  
  460.   slen = strlen (src);
  461. #ifdef VMS
  462.   if (! index (src, '/')
  463.       && (src[slen - 1] == ']'
  464.       || src[slen - 1] == ':'
  465.       || src[slen - 1] == '>'))
  466.     {
  467.       /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
  468.       fab.fab$l_fna = src;
  469.       fab.fab$b_fns = slen;
  470.       fab.fab$l_nam = &nam;
  471.       fab.fab$l_fop = FAB$M_NAM;
  472.  
  473.       nam.nam$l_esa = esa;
  474.       nam.nam$b_ess = sizeof esa;
  475.       nam.nam$b_nop |= NAM$M_SYNCHK;
  476.  
  477.       /* We call SYS$PARSE to handle such things as [--] for us. */
  478.       if (SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL)
  479.     {
  480.       slen = nam.nam$b_esl;
  481.       if (esa[slen - 1] == ';' && esa[slen - 2] == '.')
  482.         slen -= 2;
  483.       esa[slen] = '\0';
  484.       src = esa;
  485.     }
  486.       if (src[slen - 1] != ']' && src[slen - 1] != '>')
  487.     {
  488.       /* what about when we have logical_name:???? */
  489.       if (src[slen - 1] == ':')
  490.         {            /* Xlate logical name and see what we get */
  491.           ptr = strcpy (dst, src); /* upper case for getenv */
  492.           while (*ptr)
  493.         {
  494.           if ('a' <= *ptr && *ptr <= 'z')
  495.             *ptr -= 040;
  496.           ptr++;
  497.         }
  498.           dst[slen - 1] = 0;    /* remove colon */
  499.           if (!(src = egetenv (dst)))
  500.         return 0;
  501.           /* should we jump to the beginning of this procedure?
  502.          Good points: allows us to use logical names that xlate
  503.          to Unix names,
  504.          Bad points: can be a problem if we just translated to a device
  505.          name...
  506.          For now, I'll punt and always expect VMS names, and hope for
  507.          the best! */
  508.           slen = strlen (src);
  509.           if (src[slen - 1] != ']' && src[slen - 1] != '>')
  510.         { /* no recursion here! */
  511.           strcpy (dst, src);
  512.           return 0;
  513.         }
  514.         }
  515.       else
  516.         {        /* not a directory spec */
  517.           strcpy (dst, src);
  518.           return 0;
  519.         }
  520.     }
  521.       bracket = src[slen - 1];
  522.  
  523.       /* If bracket is ']' or '>', bracket - 2 is the corresponding
  524.      opening bracket.  */
  525.       ptr = index (src, bracket - 2);
  526.       if (ptr == 0)
  527.     { /* no opening bracket */
  528.       strcpy (dst, src);
  529.       return 0;
  530.     }
  531.       if (!(rptr = rindex (src, '.')))
  532.     rptr = ptr;
  533.       slen = rptr - src;
  534.       strncpy (dst, src, slen);
  535.       dst[slen] = '\0';
  536.       if (*rptr == '.')
  537.     {
  538.       dst[slen++] = bracket;
  539.       dst[slen] = '\0';
  540.     }
  541.       else
  542.     {
  543.       /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
  544.          then translate the device and recurse. */
  545.       if (dst[slen - 1] == ':'
  546.           && dst[slen - 2] != ':'    /* skip decnet nodes */
  547.           && strcmp(src + slen, "[000000]") == 0)
  548.         {
  549.           dst[slen - 1] = '\0';
  550.           if ((ptr = egetenv (dst))
  551.           && (rlen = strlen (ptr) - 1) > 0
  552.           && (ptr[rlen] == ']' || ptr[rlen] == '>')
  553.           && ptr[rlen - 1] == '.')
  554.         {
  555.           char * buf = (char *) alloca (strlen (ptr) + 1);
  556.           strcpy (buf, ptr);
  557.           buf[rlen - 1] = ']';
  558.           buf[rlen] = '\0';
  559.           return directory_file_name (buf, dst);
  560.         }
  561.           else
  562.         dst[slen - 1] = ':';
  563.         }
  564.       strcat (dst, "[000000]");
  565.       slen += 8;
  566.     }
  567.       rptr++;
  568.       rlen = strlen (rptr) - 1;
  569.       strncat (dst, rptr, rlen);
  570.       dst[slen + rlen] = '\0';
  571.       strcat (dst, ".DIR.1");
  572.       return 1;
  573.     }
  574. #endif /* VMS */
  575.   /* Process as Unix format: just remove any final slash.
  576.      But leave "/" unchanged; do not change it to "".  */
  577.   strcpy (dst, src);
  578.   if (slen > 1 && dst[slen - 1] == PATH_SEPARATOR
  579. #ifdef WINDOWSNT
  580.       /* Leave drive roots alone also */
  581.       /* UNC is broken */
  582.       && dst[slen-2] != ':'
  583. #endif
  584.       )
  585.     dst[slen - 1] = 0;
  586.   return 1;
  587. }
  588.  
  589. DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
  590.   1, 1, 0,
  591.   "Returns the file name of the directory named DIR.\n\
  592. This is the name of the file that holds the data for the directory DIR.\n\
  593. This operation exists because a directory is also a file, but its name as\n\
  594. a directory is different from its name as a file.\n\
  595. In Unix-syntax, this function just removes the final slash.\n\
  596. On VMS, given a VMS-syntax directory name such as \"[X.Y]\",\n\
  597. it returns a file name such as \"[X]Y.DIR.1\".")
  598.   (directory)
  599.      Lisp_Object directory;
  600. {
  601.   char *buf;
  602.   Lisp_Object handler;
  603.  
  604.   CHECK_STRING (directory, 0);
  605.  
  606.   if (NILP (directory))
  607.     return Qnil;
  608.  
  609.   /* If the file name has special constructs in it,
  610.      call the corresponding file handler.  */
  611.   handler = Ffind_file_name_handler (directory);
  612.   if (!NILP (handler))
  613.     return call2 (handler, Qdirectory_file_name, directory);
  614.  
  615. #ifdef VMS
  616.   /* 20 extra chars is insufficient for VMS, since we might perform a
  617.      logical name translation. an equivalence string can be up to 255
  618.      chars long, so grab that much extra space...  - sss */
  619.   buf = (char *) alloca (XSTRING (directory)->size + 20 + 255);
  620. #else
  621.   buf = (char *) alloca (XSTRING (directory)->size + 20);
  622. #endif
  623.   directory_file_name (XSTRING (directory)->data, buf);
  624.   return build_string (buf);
  625. }
  626.  
  627. DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0,
  628.   "Generate temporary file name (string) starting with PREFIX (a string).\n\
  629. The Emacs process number forms part of the result,\n\
  630. so there is no danger of generating a name being used by another process.")
  631.   (prefix)
  632.      Lisp_Object prefix;
  633. {
  634.   Lisp_Object val;
  635.   val = concat2 (prefix, build_string ("XXXXXX"));
  636.   mktemp (XSTRING (val)->data);
  637.   return val;
  638. }
  639.  
  640. DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  641.   "Convert FILENAME to absolute, and canonicalize it.\n\
  642. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  643.  (does not start with slash); if DEFAULT is nil or missing,\n\
  644. the current buffer's value of default-directory is used.\n\
  645. Path components that are `.' are removed, and \n\
  646. path components followed by `..' are removed, along with the `..' itself;\n\
  647. note that these simplifications are done without checking the resulting\n\
  648. paths in the file system.\n\
  649. An initial `~/' expands to your home directory.\n\
  650. An initial `~USER/' expands to USER's home directory.\n\
  651. See also the function `substitute-in-file-name'.")
  652.      (name, defalt)
  653.      Lisp_Object name, defalt;
  654. {
  655.   unsigned char *nm;
  656.   
  657.   register unsigned char *newdir, *p, *o;
  658.   int tlen;
  659.   unsigned char *target;
  660. #ifndef WINDOWSNT
  661.   struct passwd *pw;
  662. #endif
  663. #ifdef VMS
  664.   unsigned char * colon = 0;
  665.   unsigned char * close = 0;
  666.   unsigned char * slash = 0;
  667.   unsigned char * brack = 0;
  668.   int lbrack = 0, rbrack = 0;
  669.   int dots = 0;
  670. #endif /* VMS */
  671.   Lisp_Object handler;
  672.   
  673.   CHECK_STRING (name, 0);
  674.  
  675.   /* If the file name has special constructs in it,
  676.      call the corresponding file handler.  */
  677.   handler = Ffind_file_name_handler (name);
  678.   if (!NILP (handler))
  679.     return call3 (handler, Qexpand_file_name, name, defalt);
  680.  
  681.   /* Use the buffer's default-directory if DEFALT is omitted.  */
  682.   if (NILP (defalt))
  683.     defalt = current_buffer->directory;
  684.   CHECK_STRING (defalt, 1);
  685.  
  686.   /* Make sure DEFALT is properly expanded.
  687.      It would be better to do this down below where we actually use
  688.      defalt.  Unfortunately, calling Fexpand_file_name recursively
  689.      could invoke GC, and the strings might be relocated.  This would
  690.      be annoying because we have pointers into strings lying around
  691.      that would need adjusting, and people would add new pointers to
  692.      the code and forget to adjust them, resulting in intermittent bugs.
  693.      Putting this call here avoids all that crud.
  694.  
  695.      The EQ test avoids infinite recursion.  */
  696.   if (! NILP (defalt) && !EQ (defalt, name)
  697.       /* This saves time in a common case.  */
  698.       && XSTRING (defalt)->data[0] != PATH_SEPARATOR
  699. #ifdef WINDOWSNT
  700.       && XSTRING (defalt)->data[1] != ':'
  701. #endif
  702.       )
  703.     {
  704.       struct gcpro gcpro1;
  705.  
  706.       GCPRO1 (name);
  707.       defalt = Fexpand_file_name (defalt, Qnil);
  708.       UNGCPRO;
  709.     }
  710.  
  711. #ifdef VMS
  712.   /* Filenames on VMS are always upper case.  */
  713.   name = Fupcase (name);
  714. #endif
  715.  
  716.   nm = XSTRING (name)->data;
  717.   
  718.   /* If nm is absolute, flush ...// and detect /./ and /../.
  719.      If no /./ or /../ we can return right away. */
  720.   if (nm[0] == PATH_SEPARATOR
  721. #ifdef WINDOWSNT
  722.       || nm[1] == ':'
  723. #endif /* !WINDOWSNT */
  724. #ifdef VMS
  725.       || index (nm, ':')
  726. #endif /* VMS */
  727.       )
  728.     {
  729.       /* If it turns out that the filename we want to return is just a
  730.      suffix of FILENAME, we don't need to go through and edit
  731.      things; we just need to construct a new string using data
  732.      starting at the middle of FILENAME.  If we set lose to a
  733.      non-zero value, that means we've discovered that we can't do
  734.      that cool trick.  */
  735.       int lose = 0;
  736.  
  737.       p = nm;
  738.       while (*p)
  739.     {
  740.       /* Since we know the path is absolute, we can assume that each
  741.          element starts with a "/".  */
  742.  
  743.       /* "//" anywhere isn't necessarily hairy; we just start afresh
  744.          with the second slash.  */
  745.       if (p[0] == PATH_SEPARATOR && p[1] == PATH_SEPARATOR
  746. #ifdef APOLLO
  747.           /* // at start of filename is meaningful on Apollo system */
  748.           && nm != p
  749. #endif /* APOLLO */
  750. #ifdef WINDOWSNT
  751.           /*
  752.            * \\ at the start of a pathname is meaningful on Windows NT(tm)
  753.            */
  754.           && nm != p
  755. #endif /* WINDOWSNT */
  756.           )
  757.         nm = p + 1;
  758.  
  759.       /* "~" is hairy as the start of any path element.  */
  760.       if (p[0] == PATH_SEPARATOR && p[1] == '~')
  761.         nm = p + 1, lose = 1;
  762.  
  763.       /* "." and ".." are hairy.  */
  764.       if (p[0] == PATH_SEPARATOR
  765.           && p[1] == '.'
  766.           && (p[2] == PATH_SEPARATOR
  767.           || p[2] == 0
  768.           || (p[2] == '.' && (p[3] == PATH_SEPARATOR
  769.                       || p[3] == 0))))
  770.         lose = 1;
  771. #ifdef VMS
  772.       if (p[0] == '\\')
  773.         lose = 1;
  774.       if (p[0] == '/') {
  775.         /* if dev:[dir]/, move nm to / */
  776.         if (!slash && p > nm && (brack || colon)) {
  777.           nm = (brack ? brack + 1 : colon + 1);
  778.           lbrack = rbrack = 0;
  779.           brack = 0;
  780.           colon = 0;
  781.         }
  782.         slash = p;
  783.       }
  784.       if (p[0] == '-')
  785. #ifndef VMS4_4
  786.         /* VMS pre V4.4,convert '-'s in filenames. */
  787.         if (lbrack == rbrack)
  788.           {
  789.         if (dots < 2)    /* this is to allow negative version numbers */
  790.           p[0] = '_';
  791.           }
  792.         else
  793. #endif /* VMS4_4 */
  794.           if (lbrack > rbrack &&
  795.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  796.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  797.         lose = 1;
  798. #ifndef VMS4_4
  799.           else
  800.         p[0] = '_';
  801. #endif /* VMS4_4 */
  802.       /* count open brackets, reset close bracket pointer */
  803.       if (p[0] == '[' || p[0] == '<')
  804.         lbrack++, brack = 0;
  805.       /* count close brackets, set close bracket pointer */
  806.       if (p[0] == ']' || p[0] == '>')
  807.         rbrack++, brack = p;
  808.       /* detect ][ or >< */
  809.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  810.         lose = 1;
  811.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  812.         nm = p + 1, lose = 1;
  813.       if (p[0] == ':' && (colon || slash))
  814.         /* if dev1:[dir]dev2:, move nm to dev2: */
  815.         if (brack)
  816.           {
  817.         nm = brack + 1;
  818.         brack = 0;
  819.           }
  820.         /* if /pathname/dev:, move nm to dev: */
  821.         else if (slash)
  822.           nm = slash + 1;
  823.         /* if node::dev:, move colon following dev */
  824.         else if (colon && colon[-1] == ':')
  825.           colon = p;
  826.         /* if dev1:dev2:, move nm to dev2: */
  827.         else if (colon && colon[-1] != ':')
  828.           {
  829.         nm = colon + 1;
  830.         colon = 0;
  831.           }
  832.       if (p[0] == ':' && !colon)
  833.         {
  834.           if (p[1] == ':')
  835.         p++;
  836.           colon = p;
  837.         }
  838.       if (lbrack == rbrack)
  839.         if (p[0] == ';')
  840.           dots = 2;
  841.         else if (p[0] == '.')
  842.           dots++;
  843. #endif /* VMS */
  844.       p++;
  845.     }
  846.       if (!lose)
  847.     {
  848. #ifdef VMS
  849.       if (index (nm, '/'))
  850.         return build_string (sys_translate_unix (nm));
  851. #endif /* VMS */
  852.       if (nm == XSTRING (name)->data)
  853.         return name;
  854.       return build_string (nm);
  855.     }
  856.     }
  857.  
  858.   /* Now determine directory to start with and put it in newdir */
  859.  
  860.   newdir = 0;
  861.  
  862.   if (nm[0] == '~')        /* prefix ~ */
  863.     {
  864. #ifdef    WINDOWSNT
  865.       /*
  866.        * Under WINDOWSNT there isn't a neat way to do this yet... It should be
  867.        * possible at some point to get the necessary goodies from the registy.
  868.        */
  869.       if (nm[1] == '\\' || nm[1] == '/'
  870. #else /* !WINDOWSNT */
  871.       if (nm[1] == '/'
  872. #endif /* !WINDOWSNT */
  873. #ifdef VMS
  874.       || nm[1] == ':'
  875. #endif                /* VMS */
  876.       || nm[1] == 0)    /* ~ by itself */
  877.     {
  878.       if (!(newdir = (unsigned char *) egetenv ("HOME")))
  879.         newdir = (unsigned char *) "";
  880.       nm++;
  881. #ifdef VMS
  882.       nm++;            /* Don't leave the slash in nm.  */
  883. #endif                /* VMS */
  884.     }
  885.       else            /* ~user/filename */
  886.     {
  887.       for (p = nm; *p && 
  888.            (*p != PATH_SEPARATOR
  889. #ifdef VMS
  890.                   && *p != ':'
  891. #endif                /* VMS */
  892.                   ); p++);
  893.       o = (unsigned char *) alloca (p - nm + 1);
  894.       bcopy ((char *) nm, o, p - nm);
  895.       o [p - nm] = 0;
  896.  
  897. #ifdef    WINDOWSNT
  898.           /* DebPrint(("EMACS broken @-"__FILE__ ": %d\n", __LINE__)); */
  899.       /*
  900.        * Emacs wants to know the user's home directory...  This is set by
  901.        * the user-manager, but how do I get that information from the
  902.        * system?
  903.        *
  904.        * After a bit of hunting I discover that the user's home directroy
  905.        * is stored at:  "HKEY_LOCAL_MACHINE\\security\\sam\\"
  906.        * "domains\\account\\users\\<account-rid>\\v" in the registry...
  907.        * Now I could pull it out but this location only contains local
  908.        * accounts... so if you're logged on to some non-local domain this
  909.        * may run into a security problem... i.e. I may not always be able
  910.        * to read this information even for myself...
  911.        *
  912.        * What's here is a hack to make things work...
  913.        */
  914.  
  915.       newdir = (unsigned char *) egetenv ("HOME");
  916. #else /* !WINDOWSNT */
  917.       pw = (struct passwd *) getpwnam (o + 1);
  918.       if (pw)
  919.         {
  920.           newdir = (unsigned char *) pw -> pw_dir;
  921. #ifdef VMS
  922.           nm = p + 1;    /* skip the terminator */
  923. #else
  924.           nm = p;
  925. #endif                /* VMS */
  926.         }
  927. #endif /* !WINDOWSNT */
  928.  
  929.       /* If we don't find a user of that name, leave the name
  930.          unchanged; don't move nm forward to p.  */
  931.     }
  932.     }
  933.  
  934.   if (!IS_ANY_SEPARATOR(nm[0])
  935. #ifdef WINDOWSNT
  936.       && nm[1] != ':'
  937. #endif
  938. #ifdef VMS
  939.       && !index (nm, ':')
  940. #endif /* not VMS */
  941.       && !newdir)
  942.     {
  943.       newdir = XSTRING (defalt)->data;
  944.     }
  945.  
  946.   if (newdir != 0)
  947.     {
  948.       /* Get rid of any slash at the end of newdir.  */
  949.       int length = strlen (newdir);
  950.       /* Adding `length > 1 &&' makes ~ expand into / when homedir
  951.      is the root dir.  People disagree about whether that is right.
  952.      Anyway, we can't take the risk of this change now.  */
  953.       if (newdir[length - 1] == PATH_SEPARATOR)
  954.     {
  955.       unsigned char *temp = (unsigned char *) alloca (length);
  956.       bcopy (newdir, temp, length - 1);
  957.       temp[length - 1] = 0;
  958.       newdir = temp;
  959.     }
  960.       tlen = length + 1;
  961.     }
  962.   else
  963.     tlen = 0;
  964.  
  965.   /* Now concatenate the directory and name to new space in the stack frame */
  966.   tlen += strlen (nm) + 1;
  967.   target = (unsigned char *) alloca (tlen);
  968.   *target = 0;
  969.  
  970.   if (newdir)
  971.     {
  972. #ifndef VMS
  973.       if (nm[0] == 0 || nm[0] == PATH_SEPARATOR)
  974.     strcpy (target, newdir);
  975.       else
  976. #endif
  977.     file_name_as_directory (target, newdir);
  978.     }
  979.  
  980.   strcat (target, nm);
  981. #ifdef VMS
  982.   if (index (target, '/'))
  983.     strcpy (target, sys_translate_unix (target));
  984. #endif /* VMS */
  985.  
  986.   /* Now canonicalize by removing /. and /foo/.. if they appear.  */
  987.  
  988.   p = target;
  989.   o = target;
  990.  
  991. #ifdef WINDOWSNT
  992.   if (p[1] == ':')
  993.   {
  994.     p += 2;
  995.     o += 2;
  996.   }
  997. #endif /* WINDOWSNT */
  998.  
  999.   while (*p)
  1000.     {
  1001. #ifdef VMS
  1002.       if (*p != ']' && *p != '>' && *p != '-')
  1003.     {
  1004.       if (*p == '\\')
  1005.         p++;
  1006.       *o++ = *p++;
  1007.     }
  1008.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  1009.     /* brackets are offset from each other by 2 */
  1010.     {
  1011.       p += 2;
  1012.       if (*p != '.' && *p != '-' && o[-1] != '.')
  1013.         /* convert [foo][bar] to [bar] */
  1014.         while (o[-1] != '[' && o[-1] != '<')
  1015.           o--;
  1016.       else if (*p == '-' && *o != '.')
  1017.         *--p = '.';
  1018.     }
  1019.       else if (p[0] == '-' && o[-1] == '.' &&
  1020.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  1021.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  1022.     {
  1023.       do
  1024.         o--;
  1025.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  1026.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  1027.         p += 2;
  1028.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  1029.         p++, o--;
  1030.       /* else [foo.-] ==> [-] */
  1031.     }
  1032.       else
  1033.     {
  1034. #ifndef VMS4_4
  1035.       if (*p == '-' &&
  1036.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  1037.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  1038.         *p = '_';
  1039. #endif /* VMS4_4 */
  1040.       *o++ = *p++;
  1041.     }
  1042. #else /* not VMS */
  1043.       if (*p != PATH_SEPARATOR)
  1044.      {
  1045.       *o++ = *p++;
  1046.     }
  1047. #ifdef WINDOWSNT
  1048.       else if (!strncmp (p, "\\\\", 2)
  1049. #else /* !WINDOWSNT */
  1050.       else if (!strncmp (p, "//", 2)
  1051. #endif /* !WINDOWSNT */
  1052. #ifdef APOLLO
  1053.            /* // at start of filename is meaningful in Apollo system */
  1054.            && o != target
  1055. #endif /* APOLLO */
  1056. #ifdef WINDOWSNT
  1057.            /* \\ at start of filename is meaningful in Windows-NT */
  1058.            && o != target
  1059. #endif /* WINDOWSNT */
  1060.            )
  1061.     {
  1062.       o = target;
  1063.       p++;
  1064.     }
  1065.  
  1066.       else if (p[0] == PATH_SEPARATOR
  1067.            && p[1] == '.'
  1068.            && (p[2] == PATH_SEPARATOR
  1069.            || p[2] == 0))
  1070.     {
  1071.       /* If "/." is the entire filename, keep the "/".  Otherwise,
  1072.          just delete the whole "/.".  */
  1073.       if (o == target && p[2] == '\0')
  1074.         *o++ = *p;
  1075.       p += 2;
  1076.     }
  1077. #ifdef WINDOWSNT
  1078.       else if (!strncmp (p, "\\..", 3)
  1079. #else /* !WINDOWSNT */
  1080.       else if (!strncmp (p, "/..", 3)
  1081. #endif /* !WINDOWSNT */
  1082.            /* `/../' is the "superroot" on certain file systems.  */
  1083.            && o != target
  1084.            && (p[3] == PATH_SEPARATOR || p[3] == 0))
  1085.     {
  1086.       while (o != target && *--o != PATH_SEPARATOR)
  1087.         ;
  1088. #ifdef APOLLO
  1089.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  1090.         ++o;
  1091.       else
  1092. #endif /* APOLLO */
  1093. #ifdef WINDOWSNT
  1094.       if (o == target + 1 && o[-1] == '\\' && o[0] == '\\')
  1095.         ++o;
  1096.       else
  1097. #endif /* !WINDOWSNT */
  1098.       if (o == target && IS_ANY_SEPARATOR(*o))
  1099.         ++o;
  1100.       p += 3;
  1101.     }
  1102.       else
  1103.      {
  1104.       *o++ = *p++;
  1105.     }
  1106. #endif /* not VMS */
  1107.     }
  1108.  
  1109.   return make_string (target, o - target);
  1110. }
  1111. #if 0
  1112. /* Changed this DEFUN to a DEAFUN, so as not to confuse `make-docfile'.
  1113. DEAFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  1114.   "Convert FILENAME to absolute, and canonicalize it.\n\
  1115. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  1116.  (does not start with slash); if DEFAULT is nil or missing,\n\
  1117. the current buffer's value of default-directory is used.\n\
  1118. Filenames containing `.' or `..' as components are simplified;\n\
  1119. initial `~/' expands to your home directory.\n\
  1120. See also the function `substitute-in-file-name'.")
  1121.      (name, defalt)
  1122.      Lisp_Object name, defalt;
  1123. {
  1124.   unsigned char *nm;
  1125.   
  1126.   register unsigned char *newdir, *p, *o;
  1127.   int tlen;
  1128.   unsigned char *target;
  1129.   struct passwd *pw;
  1130.   int lose;
  1131. #ifdef VMS
  1132.   unsigned char * colon = 0;
  1133.   unsigned char * close = 0;
  1134.   unsigned char * slash = 0;
  1135.   unsigned char * brack = 0;
  1136.   int lbrack = 0, rbrack = 0;
  1137.   int dots = 0;
  1138. #endif /* VMS */
  1139.   
  1140.   CHECK_STRING (name, 0);
  1141.  
  1142. #ifdef VMS
  1143.   /* Filenames on VMS are always upper case.  */
  1144.   name = Fupcase (name);
  1145. #endif
  1146.  
  1147.   nm = XSTRING (name)->data;
  1148.   
  1149.   /* If nm is absolute, flush ...// and detect /./ and /../.
  1150.      If no /./ or /../ we can return right away. */
  1151.   if (IS_ANY_SEPARATOR(nm[0])
  1152. #ifdef VMS
  1153.       || index (nm, ':')
  1154. #endif /* VMS */
  1155.       )
  1156.     {
  1157.       p = nm;
  1158.       lose = 0;
  1159.       while (*p)
  1160.     {
  1161.       if (p[0] == '/' && p[1] == '/'
  1162. #ifdef APOLLO
  1163.           /* // at start of filename is meaningful on Apollo system */
  1164.           && nm != p
  1165. #endif /* APOLLO */
  1166.           )
  1167.         nm = p + 1;
  1168.       if (p[0] == '/' && p[1] == '~')
  1169.         nm = p + 1, lose = 1;
  1170.       if (p[0] == '/' && p[1] == '.'
  1171.           && (p[2] == '/' || p[2] == 0
  1172.           || (p[2] == '.' && (p[3] == '/' || p[3] == 0))))
  1173.         lose = 1;
  1174. #ifdef VMS
  1175.       if (p[0] == '\\')
  1176.         lose = 1;
  1177.       if (p[0] == '/') {
  1178.         /* if dev:[dir]/, move nm to / */
  1179.         if (!slash && p > nm && (brack || colon)) {
  1180.           nm = (brack ? brack + 1 : colon + 1);
  1181.           lbrack = rbrack = 0;
  1182.           brack = 0;
  1183.           colon = 0;
  1184.         }
  1185.         slash = p;
  1186.       }
  1187.       if (p[0] == '-')
  1188. #ifndef VMS4_4
  1189.         /* VMS pre V4.4,convert '-'s in filenames. */
  1190.         if (lbrack == rbrack)
  1191.           {
  1192.         if (dots < 2)    /* this is to allow negative version numbers */
  1193.           p[0] = '_';
  1194.           }
  1195.         else
  1196. #endif /* VMS4_4 */
  1197.           if (lbrack > rbrack &&
  1198.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  1199.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  1200.         lose = 1;
  1201. #ifndef VMS4_4
  1202.           else
  1203.         p[0] = '_';
  1204. #endif /* VMS4_4 */
  1205.       /* count open brackets, reset close bracket pointer */
  1206.       if (p[0] == '[' || p[0] == '<')
  1207.         lbrack++, brack = 0;
  1208.       /* count close brackets, set close bracket pointer */
  1209.       if (p[0] == ']' || p[0] == '>')
  1210.         rbrack++, brack = p;
  1211.       /* detect ][ or >< */
  1212.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  1213.         lose = 1;
  1214.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  1215.         nm = p + 1, lose = 1;
  1216.       if (p[0] == ':' && (colon || slash))
  1217.         /* if dev1:[dir]dev2:, move nm to dev2: */
  1218.         if (brack)
  1219.           {
  1220.         nm = brack + 1;
  1221.         brack = 0;
  1222.           }
  1223.         /* if /pathname/dev:, move nm to dev: */
  1224.         else if (slash)
  1225.           nm = slash + 1;
  1226.         /* if node::dev:, move colon following dev */
  1227.         else if (colon && colon[-1] == ':')
  1228.           colon = p;
  1229.         /* if dev1:dev2:, move nm to dev2: */
  1230.         else if (colon && colon[-1] != ':')
  1231.           {
  1232.         nm = colon + 1;
  1233.         colon = 0;
  1234.           }
  1235.       if (p[0] == ':' && !colon)
  1236.         {
  1237.           if (p[1] == ':')
  1238.         p++;
  1239.           colon = p;
  1240.         }
  1241.       if (lbrack == rbrack)
  1242.         if (p[0] == ';')
  1243.           dots = 2;
  1244.         else if (p[0] == '.')
  1245.           dots++;
  1246. #endif /* VMS */
  1247.       p++;
  1248.     }
  1249.       if (!lose)
  1250.     {
  1251. #ifdef VMS
  1252.       if (index (nm, '/'))
  1253.         return build_string (sys_translate_unix (nm));
  1254. #endif /* VMS */
  1255.       if (nm == XSTRING (name)->data)
  1256.         return name;
  1257.       return build_string (nm);
  1258.     }
  1259.     }
  1260.  
  1261.   /* Now determine directory to start with and put it in NEWDIR */
  1262.  
  1263.   newdir = 0;
  1264.  
  1265.   if (nm[0] == '~')        /* prefix ~ */
  1266.     if (nm[1] == '/'
  1267. #ifdef VMS
  1268.     || nm[1] == ':'
  1269. #endif /* VMS */
  1270.     || nm[1] == 0)/* ~/filename */
  1271.       {
  1272.     if (!(newdir = (unsigned char *) egetenv ("HOME")))
  1273.       newdir = (unsigned char *) "";
  1274.     nm++;
  1275. #ifdef VMS
  1276.     nm++;            /* Don't leave the slash in nm.  */
  1277. #endif /* VMS */
  1278.       }
  1279.     else  /* ~user/filename */
  1280.       {
  1281.     /* Get past ~ to user */
  1282.     unsigned char *user = nm + 1;
  1283.     /* Find end of name. */
  1284.     unsigned char *ptr = (unsigned char *) index (user, '/');
  1285.     int len = ptr ? ptr - user : strlen (user);
  1286. #ifdef VMS
  1287.     unsigned char *ptr1 = index (user, ':');
  1288.     if (ptr1 != 0 && ptr1 - user < len)
  1289.       len = ptr1 - user;
  1290. #endif                /* VMS */
  1291.     /* Copy the user name into temp storage. */
  1292.     o = (unsigned char *) alloca (len + 1);
  1293.     bcopy ((char *) user, o, len);
  1294.     o[len] = 0;
  1295.  
  1296.     /* Look up the user name. */
  1297.     pw = (struct passwd *) getpwnam (o + 1);
  1298.     if (!pw)
  1299.       error ("\"%s\" isn't a registered user", o + 1);
  1300.  
  1301.     newdir = (unsigned char *) pw->pw_dir;
  1302.  
  1303.     /* Discard the user name from NM.  */
  1304.     nm += len;
  1305.       }
  1306.  
  1307.   if (nm[0] != '/'
  1308. #ifdef VMS
  1309.       && !index (nm, ':')
  1310. #endif /* not VMS */
  1311.       && !newdir)
  1312.     {
  1313.       if (NILP (defalt))
  1314.     defalt = current_buffer->directory;
  1315.       CHECK_STRING (defalt, 1);
  1316.       newdir = XSTRING (defalt)->data;
  1317.     }
  1318.  
  1319.   /* Now concatenate the directory and name to new space in the stack frame */
  1320.  
  1321.   tlen = (newdir ? strlen (newdir) + 1 : 0) + strlen (nm) + 1;
  1322.   target = (unsigned char *) alloca (tlen);
  1323.   *target = 0;
  1324.  
  1325.   if (newdir)
  1326.     {
  1327. #ifndef VMS
  1328.       if (nm[0] == 0 || nm[0] == '/')
  1329.     strcpy (target, newdir);
  1330.       else
  1331. #endif
  1332.       file_name_as_directory (target, newdir);
  1333.     }
  1334.  
  1335.   strcat (target, nm);
  1336. #ifdef VMS
  1337.   if (index (target, '/'))
  1338.     strcpy (target, sys_translate_unix (target));
  1339. #endif /* VMS */
  1340.  
  1341.   /* Now canonicalize by removing /. and /foo/.. if they appear */
  1342.  
  1343.   p = target;
  1344.   o = target;
  1345.  
  1346.   while (*p)
  1347.     {
  1348. #ifdef VMS
  1349.       if (*p != ']' && *p != '>' && *p != '-')
  1350.     {
  1351.       if (*p == '\\')
  1352.         p++;
  1353.       *o++ = *p++;
  1354.     }
  1355.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  1356.     /* brackets are offset from each other by 2 */
  1357.     {
  1358.       p += 2;
  1359.       if (*p != '.' && *p != '-' && o[-1] != '.')
  1360.         /* convert [foo][bar] to [bar] */
  1361.         while (o[-1] != '[' && o[-1] != '<')
  1362.           o--;
  1363.       else if (*p == '-' && *o != '.')
  1364.         *--p = '.';
  1365.     }
  1366.       else if (p[0] == '-' && o[-1] == '.' &&
  1367.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  1368.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  1369.     {
  1370.       do
  1371.         o--;
  1372.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  1373.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  1374.         p += 2;
  1375.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  1376.         p++, o--;
  1377.       /* else [foo.-] ==> [-] */
  1378.     }
  1379.       else
  1380.     {
  1381. #ifndef VMS4_4
  1382.       if (*p == '-' &&
  1383.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  1384.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  1385.         *p = '_';
  1386. #endif /* VMS4_4 */
  1387.       *o++ = *p++;
  1388.     }
  1389. #else /* not VMS */
  1390.       if (*p != '/')
  1391.      {
  1392.       *o++ = *p++;
  1393.     }
  1394.       else if (!strncmp (p, "//", 2)
  1395. #ifdef APOLLO
  1396.            /* // at start of filename is meaningful in Apollo system */
  1397.            && o != target
  1398. #endif /* APOLLO */
  1399.            )
  1400.     {
  1401.       o = target;
  1402.       p++;
  1403.     }
  1404.       else if (p[0] == '/' && p[1] == '.' &&
  1405.            (p[2] == '/' || p[2] == 0))
  1406.     p += 2;
  1407.       else if (!strncmp (p, "/..", 3)
  1408.            /* `/../' is the "superroot" on certain file systems.  */
  1409.            && o != target
  1410.            && (p[3] == '/' || p[3] == 0))
  1411.     {
  1412.       while (o != target && *--o != '/')
  1413.         ;
  1414. #ifdef APOLLO
  1415.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  1416.         ++o;
  1417.       else
  1418. #endif /* APOLLO */
  1419.       if (o == target && *o == '/')
  1420.         ++o;
  1421.       p += 3;
  1422.     }
  1423.       else
  1424.      {
  1425.       *o++ = *p++;
  1426.     }
  1427. #endif /* not VMS */
  1428.     }
  1429.  
  1430.   return make_string (target, o - target);
  1431. }
  1432. #endif
  1433.  
  1434. DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name,
  1435.   Ssubstitute_in_file_name, 1, 1, 0,
  1436.   "Substitute environment variables referred to in FILENAME.\n\
  1437. `$FOO' where FOO is an environment variable name means to substitute\n\
  1438. the value of that variable.  The variable name should be terminated\n\
  1439. with a character not a letter, digit or underscore; otherwise, enclose\n\
  1440. the entire variable name in braces.\n\
  1441. If `/~' appears, all of FILENAME through that `/' is discarded.\n\n\
  1442. On VMS, `$' substitution is not done; this function does little and only\n\
  1443. duplicates what `expand-file-name' does.")
  1444.   (string)
  1445.      Lisp_Object string;
  1446. {
  1447.   unsigned char *nm;
  1448.  
  1449.   register unsigned char *s, *p, *o, *x, *endp;
  1450.   unsigned char *target;
  1451.   int total = 0;
  1452.   int substituted = 0;
  1453.   unsigned char *xnm;
  1454.  
  1455.   CHECK_STRING (string, 0);
  1456.  
  1457.   nm = XSTRING (string)->data;
  1458.   endp = nm + XSTRING (string)->size;
  1459.  
  1460.   /* If /~ or // appears, discard everything through first slash. */
  1461.  
  1462.   for (p = nm; p != endp; p++)
  1463.     {
  1464.       if ((p[0] == '~' ||
  1465. #ifdef APOLLO
  1466.        /* // at start of file name is meaningful in Apollo system */
  1467.        (p[0] == PATH_SEPARATOR && p - 1 != nm)
  1468. #elif WINDOWSNT
  1469.        (p[0] == '\\' && p - 1 != nm)
  1470. #else /* !(APOLLO || WINDOWSNT) */
  1471.        p[0] == '/'
  1472. #endif /* !(APOLLO || WINDOWSNT) */
  1473.        )
  1474.       && p != nm &&
  1475. #ifdef VMS
  1476.       (p[-1] == ':' || p[-1] == ']' || p[-1] == '>' ||
  1477. #endif /* VMS */
  1478.       p[-1] == PATH_SEPARATOR)
  1479. #ifdef VMS
  1480.       )
  1481. #endif /* VMS */
  1482.     {
  1483.       nm = p;
  1484.       substituted = 1;
  1485.     }
  1486.     }
  1487.  
  1488. #ifdef VMS
  1489.   return build_string (nm);
  1490. #else
  1491.  
  1492.   /* See if any variables are substituted into the string
  1493.      and find the total length of their values in `total' */
  1494.  
  1495.   for (p = nm; p != endp;)
  1496.     if (*p != '$')
  1497.       p++;
  1498.     else
  1499.       {
  1500.     p++;
  1501.     if (p == endp)
  1502.       goto badsubst;
  1503.     else if (*p == '$')
  1504.       {
  1505.         /* "$$" means a single "$" */
  1506.         p++;
  1507.         total -= 1;
  1508.         substituted = 1;
  1509.         continue;
  1510.       }
  1511.     else if (*p == '{')
  1512.       {
  1513.         o = ++p;
  1514.         while (p != endp && *p != '}') p++;
  1515.         if (*p != '}') goto missingclose;
  1516.         s = p;
  1517.       }
  1518.     else
  1519.       {
  1520.         o = p;
  1521.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1522.         s = p;
  1523.       }
  1524.  
  1525.     /* Copy out the variable name */
  1526.     target = (unsigned char *) alloca (s - o + 1);
  1527.     strncpy (target, o, s - o);
  1528.     target[s - o] = 0;
  1529.  
  1530.     /* Get variable value */
  1531.     o = (unsigned char *) egetenv (target);
  1532. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1533. #if 0
  1534. #ifdef USG
  1535.     if (!o && !strcmp (target, "USER"))
  1536.       o = egetenv ("LOGNAME");
  1537. #endif /* USG */
  1538. #endif /* 0 */
  1539.     if (!o) goto badvar;
  1540.     total += strlen (o);
  1541.     substituted = 1;
  1542.       }
  1543.  
  1544.   if (!substituted)
  1545.     return string;
  1546.  
  1547.   /* If substitution required, recopy the string and do it */
  1548.   /* Make space in stack frame for the new copy */
  1549.   xnm = (unsigned char *) alloca (XSTRING (string)->size + total + 1);
  1550.   x = xnm;
  1551.  
  1552.   /* Copy the rest of the name through, replacing $ constructs with values */
  1553.   for (p = nm; *p;)
  1554.     if (*p != '$')
  1555.       *x++ = *p++;
  1556.     else
  1557.       {
  1558.     p++;
  1559.     if (p == endp)
  1560.       goto badsubst;
  1561.     else if (*p == '$')
  1562.       {
  1563.         *x++ = *p++;
  1564.         continue;
  1565.       }
  1566.     else if (*p == '{')
  1567.       {
  1568.         o = ++p;
  1569.         while (p != endp && *p != '}') p++;
  1570.         if (*p != '}') goto missingclose;
  1571.         s = p++;
  1572.       }
  1573.     else
  1574.       {
  1575.         o = p;
  1576.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1577.         s = p;
  1578.       }
  1579.  
  1580.     /* Copy out the variable name */
  1581.     target = (unsigned char *) alloca (s - o + 1);
  1582.     strncpy (target, o, s - o);
  1583.     target[s - o] = 0;
  1584.  
  1585.     /* Get variable value */
  1586.     o = (unsigned char *) egetenv (target);
  1587. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1588. #if 0
  1589. #ifdef USG
  1590.     if (!o && !strcmp (target, "USER"))
  1591.       o = egetenv ("LOGNAME");
  1592. #endif /* USG */
  1593. #endif /* 0 */
  1594.     if (!o)
  1595.       goto badvar;
  1596.  
  1597.     strcpy (x, o);
  1598.     x += strlen (o);
  1599.       }
  1600.  
  1601.   *x = 0;
  1602.  
  1603.   /* If /~ or // appears, discard everything through first slash. */
  1604.  
  1605.   for (p = xnm; p != x; p++)
  1606.     if ((p[0] == '~' ||
  1607. #ifdef APOLLO
  1608.      /* // at start of file name is meaningful in Apollo system */
  1609.      (p[0] == '/' && p - 1 != xnm)
  1610. #elif WINDOWSNT
  1611.      (p[0] == '\\' && p - 1 != xnm)
  1612. #else /* !(APOLLO || WINDOWSNT) */
  1613.      p[0] == '/'
  1614. #endif /* !(APOLLO || WINDOWSNT) */
  1615.      )
  1616.     && p != nm && p[-1] == PATH_SEPARATOR)
  1617.       xnm = p;
  1618.  
  1619.   return make_string (xnm, x - xnm);
  1620.  
  1621.  badsubst:
  1622.   error ("Bad format environment-variable substitution");
  1623.  missingclose:
  1624.   error ("Missing \"}\" in environment-variable substitution");
  1625.  badvar:
  1626.   error ("Substituting nonexistent environment variable \"%s\"", target);
  1627.  
  1628.   /* NOTREACHED */
  1629. #endif /* not VMS */
  1630. }
  1631.  
  1632. /* A slightly faster and more convenient way to get
  1633.    (directory-file-name (expand-file-name FOO)).  The return value may
  1634.    have had its last character zapped with a '\0' character, meaning
  1635.    that it is acceptable to system calls, but not to other lisp
  1636.    functions.  Callers should make sure that the return value doesn't
  1637.    escape.  */
  1638.  
  1639. Lisp_Object
  1640. expand_and_dir_to_file (filename, defdir)
  1641.      Lisp_Object filename, defdir;
  1642. {
  1643.   register Lisp_Object abspath;
  1644.  
  1645.   abspath = Fexpand_file_name (filename, defdir);
  1646. #ifdef VMS
  1647.   {
  1648.     register int c = XSTRING (abspath)->data[XSTRING (abspath)->size - 1];
  1649.     if (c == ':' || c == ']' || c == '>')
  1650.       abspath = Fdirectory_file_name (abspath);
  1651.   }
  1652. #else
  1653.   /* Remove final slash, if any (unless path is root).
  1654.      stat behaves differently depending!  */
  1655.   if (XSTRING (abspath)->size > 1
  1656.       && XSTRING (abspath)->data[XSTRING (abspath)->size - 1] ==
  1657.       PATH_SEPARATOR
  1658. #ifdef WINDOWSNT
  1659.       && XSTRING(abspath)->data[XSTRING(abspath)->size-2] != ':'
  1660. #endif
  1661.       )
  1662.     {
  1663.       if (EQ (abspath, filename))
  1664.     abspath = Fcopy_sequence (abspath);
  1665.       XSTRING (abspath)->data[XSTRING (abspath)->size - 1] = 0;
  1666.     }
  1667. #endif
  1668.   return abspath;
  1669. }
  1670.  
  1671. _VOID_
  1672. barf_or_query_if_file_exists (absname, querystring, interactive)
  1673.      Lisp_Object absname;
  1674.      unsigned char *querystring;
  1675.      int interactive;
  1676. {
  1677.   register Lisp_Object tem;
  1678.   struct gcpro gcpro1;
  1679.  
  1680.   if (access (XSTRING (absname)->data, 4) >= 0)
  1681.     {
  1682.       if (! interactive)
  1683.     Fsignal (Qfile_already_exists,
  1684.          Fcons (build_string ("File already exists"),
  1685.             Fcons (absname, Qnil)));
  1686.       GCPRO1 (absname);
  1687.       tem = do_yes_or_no_p (format1 ("File %s already exists; %s anyway? ",
  1688.                      XSTRING (absname)->data, querystring));
  1689.       UNGCPRO;
  1690.       if (NILP (tem))
  1691.     Fsignal (Qfile_already_exists,
  1692.          Fcons (build_string ("File already exists"),
  1693.             Fcons (absname, Qnil)));
  1694.     }
  1695.   return;
  1696. }
  1697.  
  1698. DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4,
  1699.   "fCopy file: \nFCopy %s to file: \np\nP",
  1700.   "Copy FILE to NEWNAME.  Both args must be strings.\n\
  1701. Signals a `file-already-exists' error if file NEWNAME already exists,\n\
  1702. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.\n\
  1703. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1704. This is what happens in interactive use with M-x.\n\
  1705. Fourth arg KEEP-TIME non-nil means give the new file the same\n\
  1706. last-modified time as the old one.  (This works on only some systems.)\n\
  1707. A prefix arg makes KEEP-TIME non-nil.")
  1708.   (filename, newname, ok_if_already_exists, keep_date)
  1709.      Lisp_Object filename, newname, ok_if_already_exists, keep_date;
  1710. {
  1711.   int ifd, ofd, n;
  1712.   char buf[16 * 1024];
  1713.   struct stat st;
  1714.   Lisp_Object handler;
  1715.   struct gcpro gcpro1, gcpro2;
  1716.   int count = specpdl_ptr - specpdl;
  1717.  
  1718.   GCPRO2 (filename, newname);
  1719.   CHECK_STRING (filename, 0);
  1720.   CHECK_STRING (newname, 1);
  1721.   filename = Fexpand_file_name (filename, Qnil);
  1722.   newname = Fexpand_file_name (newname, Qnil);
  1723.  
  1724.   /* If the input file name has special constructs in it,
  1725.      call the corresponding file handler.  */
  1726.   handler = Ffind_file_name_handler (filename);
  1727.   /* Likewise for output file name.  */
  1728.   if (NILP (handler))
  1729.     handler = Ffind_file_name_handler (newname);
  1730.   if (!NILP (handler))
  1731.     return call5 (handler, Qcopy_file, filename, newname,
  1732.           ok_if_already_exists, keep_date);
  1733.  
  1734.   if (NILP (ok_if_already_exists)
  1735.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1736.     barf_or_query_if_file_exists (newname, "copy to it",
  1737.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1738.  
  1739.   ifd = open (XSTRING (filename)->data, 0);
  1740.   if (ifd < 0)
  1741.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1742.  
  1743.   record_unwind_protect (close_file_unwind, make_number (ifd));
  1744.  
  1745. #ifdef VMS
  1746.   /* Create the copy file with the same record format as the input file */
  1747.   ofd = sys_creat (XSTRING (newname)->data, 0666, ifd);
  1748. #else
  1749.   ofd = creat (XSTRING (newname)->data, 0666);
  1750. #endif /* VMS */
  1751.   if (ofd < 0)
  1752.       report_file_error ("Opening output file", Fcons (newname, Qnil));
  1753.  
  1754.   record_unwind_protect (close_file_unwind, make_number (ofd));
  1755.  
  1756.   immediate_quit = 1;
  1757.   QUIT;
  1758.   while ((n = read (ifd, buf, sizeof buf)) > 0)
  1759.     if (write (ofd, buf, n) != n)
  1760.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1761.   immediate_quit = 0;
  1762.  
  1763.   if (fstat (ifd, &st) >= 0)
  1764.     {
  1765.       if (!NILP (keep_date))
  1766.     {
  1767.       EMACS_TIME atime, mtime;
  1768.       EMACS_SET_SECS_USECS (atime, st.st_atime, 0);
  1769.       EMACS_SET_SECS_USECS (mtime, st.st_mtime, 0);
  1770. #ifndef WINDOWSNT
  1771.       EMACS_SET_UTIMES (XSTRING (newname)->data, atime, mtime);
  1772. #endif /* !WINDOWSNT */
  1773.     }
  1774. #ifdef APOLLO
  1775.       if (!egetenv ("USE_DOMAIN_ACLS"))
  1776. #endif
  1777.     chmod (XSTRING (newname)->data, st.st_mode & 07777);
  1778.     }
  1779.  
  1780.   /* Discard the unwind protects.  */
  1781.   specpdl_ptr = specpdl + count;
  1782.  
  1783.   close (ifd);
  1784.   if (close (ofd) < 0)
  1785.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1786.  
  1787.   UNGCPRO;
  1788.   return Qnil;
  1789. }
  1790.  
  1791. DEFUN ("make-directory-internal", Fmake_directory_internal,
  1792.        Smake_directory_internal, 1, 1, 0,
  1793.   "Create a directory.  One argument, a file name string.")
  1794.   (dirname)
  1795.      Lisp_Object dirname;
  1796. {
  1797.   unsigned char *dir;
  1798.   Lisp_Object handler;
  1799.  
  1800.   CHECK_STRING (dirname, 0);
  1801.   dirname = Fexpand_file_name (dirname, Qnil);
  1802.  
  1803.   handler = Ffind_file_name_handler (dirname);
  1804.   if (!NILP (handler))
  1805.     return call3 (handler, Qmake_directory, dirname, Qnil);
  1806.  
  1807.   dir = XSTRING (dirname)->data;
  1808.  
  1809. #ifdef WINDOWSNT
  1810.   if (mkdir (dir) != 0)
  1811. #else
  1812.   if (mkdir (dir, 0777) != 0)
  1813. #endif
  1814.     report_file_error ("Creating directory", Flist (1, &dirname));
  1815.  
  1816.   return Qnil;
  1817. }
  1818.  
  1819. DEFUN ("delete-directory", Fdelete_directory, Sdelete_directory, 1, 1, "FDelete directory: ",
  1820.   "Delete a directory.  One argument, a file name string.")
  1821.   (dirname)
  1822.      Lisp_Object dirname;
  1823. {
  1824.   unsigned char *dir;
  1825.   Lisp_Object handler;
  1826.  
  1827.   CHECK_STRING (dirname, 0);
  1828.   dirname = Fexpand_file_name (dirname, Qnil);
  1829.   dir = XSTRING (dirname)->data;
  1830.  
  1831.   handler = Ffind_file_name_handler (dirname);
  1832.   if (!NILP (handler))
  1833.     return call2 (handler, Qdelete_directory, dirname);
  1834.  
  1835.   if (rmdir (dir) != 0)
  1836.     report_file_error ("Removing directory", Flist (1, &dirname));
  1837.  
  1838.   return Qnil;
  1839. }
  1840.  
  1841. DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ",
  1842.   "Delete specified file.  One argument, a file name string.\n\
  1843. If file has multiple names, it continues to exist with the other names.")
  1844.   (filename)
  1845.      Lisp_Object filename;
  1846. {
  1847.   Lisp_Object handler;
  1848.   CHECK_STRING (filename, 0);
  1849.   filename = Fexpand_file_name (filename, Qnil);
  1850.  
  1851.   handler = Ffind_file_name_handler (filename);
  1852.   if (!NILP (handler))
  1853.     return call2 (handler, Qdelete_file, filename);
  1854.  
  1855.   if (0 > unlink (XSTRING (filename)->data))
  1856.     report_file_error ("Removing old name", Flist (1, &filename));
  1857.   return Qnil;
  1858. }
  1859.  
  1860. DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
  1861.   "fRename file: \nFRename %s to file: \np",
  1862.   "Rename FILE as NEWNAME.  Both args strings.\n\
  1863. If file has names other than FILE, it continues to have those names.\n\
  1864. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  1865. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1866. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1867. This is what happens in interactive use with M-x.")
  1868.   (filename, newname, ok_if_already_exists)
  1869.      Lisp_Object filename, newname, ok_if_already_exists;
  1870. {
  1871. #ifdef NO_ARG_ARRAY
  1872.   Lisp_Object args[2];
  1873. #endif
  1874.   Lisp_Object handler;
  1875.   struct gcpro gcpro1, gcpro2;
  1876.  
  1877.   GCPRO2 (filename, newname);
  1878.   CHECK_STRING (filename, 0);
  1879.   CHECK_STRING (newname, 1);
  1880.   filename = Fexpand_file_name (filename, Qnil);
  1881.   newname = Fexpand_file_name (newname, Qnil);
  1882.  
  1883.   /* If the file name has special constructs in it,
  1884.      call the corresponding file handler.  */
  1885.   handler = Ffind_file_name_handler (filename);
  1886.   if (NILP (handler))
  1887.     handler = Ffind_file_name_handler (newname);
  1888.   if (!NILP (handler))
  1889.     return call4 (handler, Qrename_file,
  1890.           filename, newname, ok_if_already_exists);
  1891.  
  1892.   if (NILP (ok_if_already_exists)
  1893.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1894.     barf_or_query_if_file_exists (newname, "rename to it",
  1895.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1896. #ifndef BSD4_1
  1897.   if (0 > rename (XSTRING (filename)->data, XSTRING (newname)->data))
  1898. #else
  1899. #ifdef WINDOWSNT
  1900.   if (!MoveFile(XSTRING (filename)->data, XSTRING (newname)->data))
  1901. #else /* !WINDOWSNT */
  1902.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data)
  1903.       || 0 > unlink (XSTRING (filename)->data))
  1904. #endif /* !WINDOWSNT */
  1905. #endif
  1906.     {
  1907. #ifdef    WINDOWSNT
  1908.       /*
  1909.        * Why two?  And why don't they document what MoveFile will return?
  1910.        */
  1911.       if ((GetLastError() == ERROR_FILE_EXISTS) ||
  1912.       (GetLastError() == ERROR_ALREADY_EXISTS))
  1913. #else /* !WINDOWSNT */
  1914.       if (errno == EXDEV)
  1915. #endif /* !WINDOWSNT */
  1916.     {
  1917.       Fcopy_file (filename, newname,
  1918.               /* We have already prompted if it was an integer,
  1919.              so don't have copy-file prompt again.  */
  1920.               NILP (ok_if_already_exists) ? Qnil : Qt, Qt);
  1921.       Fdelete_file (filename);
  1922.     }
  1923.       else
  1924. #ifdef NO_ARG_ARRAY
  1925.     {
  1926.       args[0] = filename;
  1927.       args[1] = newname;
  1928.       report_file_error ("Renaming", Flist (2, args));
  1929.     }
  1930. #else
  1931.     report_file_error ("Renaming", Flist (2, &filename));
  1932. #endif
  1933.     }
  1934.   UNGCPRO;
  1935.   return Qnil;
  1936. }
  1937.  
  1938. DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3,
  1939.   "fAdd name to file: \nFName to add to %s: \np",
  1940.   "Give FILE additional name NEWNAME.  Both args strings.\n\
  1941. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  1942. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1943. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1944. This is what happens in interactive use with M-x.")
  1945.   (filename, newname, ok_if_already_exists)
  1946.      Lisp_Object filename, newname, ok_if_already_exists;
  1947. {
  1948. #ifndef WINDOWSNT
  1949. #ifdef NO_ARG_ARRAY
  1950.   Lisp_Object args[2];
  1951. #endif
  1952. #endif
  1953.   Lisp_Object handler;
  1954.   struct gcpro gcpro1, gcpro2;
  1955.  
  1956.   GCPRO2 (filename, newname);
  1957.   CHECK_STRING (filename, 0);
  1958.   CHECK_STRING (newname, 1);
  1959.   filename = Fexpand_file_name (filename, Qnil);
  1960.   newname = Fexpand_file_name (newname, Qnil);
  1961.  
  1962.   /* If the file name has special constructs in it,
  1963.      call the corresponding file handler.  */
  1964.   handler = Ffind_file_name_handler (filename);
  1965.   if (!NILP (handler))
  1966.     return call4 (handler, Qadd_name_to_file, filename, newname,
  1967.           ok_if_already_exists);
  1968.  
  1969.   if (NILP (ok_if_already_exists)
  1970.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1971.     barf_or_query_if_file_exists (newname, "make it a new name",
  1972.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1973.   unlink (XSTRING (newname)->data);
  1974. #ifdef    WINDOWSNT
  1975.   /*
  1976.    * Posix makes links on NTFS...  I just don't know how to do it here.  Maybe
  1977.    * spawn a posix app to do it? (bleech!)
  1978.    */
  1979.   DebPrint(("EMACS broken @ "__FILE__ ": %d\n", __LINE__));
  1980. #else /* !WINDOWSNT */
  1981.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data))
  1982.     {
  1983. #ifdef NO_ARG_ARRAY
  1984.       args[0] = filename;
  1985.       args[1] = newname;
  1986.       report_file_error ("Adding new name", Flist (2, args));
  1987. #else
  1988.       report_file_error ("Adding new name", Flist (2, &filename));
  1989. #endif
  1990.     }
  1991. #endif /* !WINDOWSNT */
  1992.  
  1993.   UNGCPRO;
  1994.   return Qnil;
  1995. }
  1996.  
  1997. #ifdef S_IFLNK
  1998. DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3,
  1999.   "FMake symbolic link to file: \nFMake symbolic link to file %s: \np",
  2000.   "Make a symbolic link to FILENAME, named LINKNAME.  Both args strings.\n\
  2001. Signals a `file-already-exists' error if a file NEWNAME already exists\n\
  2002. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  2003. A number as third arg means request confirmation if NEWNAME already exists.\n\
  2004. This happens for interactive use with M-x.")
  2005.   (filename, linkname, ok_if_already_exists)
  2006.      Lisp_Object filename, linkname, ok_if_already_exists;
  2007. {
  2008. #ifdef NO_ARG_ARRAY
  2009.   Lisp_Object args[2];
  2010. #endif
  2011.   Lisp_Object handler;
  2012.   struct gcpro gcpro1, gcpro2;
  2013.  
  2014.   GCPRO2 (filename, linkname);
  2015.   CHECK_STRING (filename, 0);
  2016.   CHECK_STRING (linkname, 1);
  2017. #if 0 /* This made it impossible to make a link to a relative name.  */
  2018.   filename = Fexpand_file_name (filename, Qnil);
  2019. #endif
  2020.   linkname = Fexpand_file_name (linkname, Qnil);
  2021.  
  2022.   /* If the file name has special constructs in it,
  2023.      call the corresponding file handler.  */
  2024.   handler = Ffind_file_name_handler (filename);
  2025.   if (!NILP (handler))
  2026.     return call4 (handler, Qmake_symbolic_link, filename, linkname,
  2027.           ok_if_already_exists);
  2028.  
  2029.   if (NILP (ok_if_already_exists)
  2030.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  2031.     barf_or_query_if_file_exists (linkname, "make it a link",
  2032.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  2033.   if (0 > symlink (XSTRING (filename)->data, XSTRING (linkname)->data))
  2034.     {
  2035.       /* If we didn't complain already, silently delete existing file.  */
  2036.       if (errno == EEXIST)
  2037.     {
  2038.       unlink (XSTRING (linkname)->data);
  2039.       if (0 <= symlink (XSTRING (filename)->data, XSTRING (linkname)->data))
  2040.         return Qnil;
  2041.     }
  2042.  
  2043. #ifdef NO_ARG_ARRAY
  2044.       args[0] = filename;
  2045.       args[1] = linkname;
  2046.       report_file_error ("Making symbolic link", Flist (2, args));
  2047. #else
  2048.       report_file_error ("Making symbolic link", Flist (2, &filename));
  2049. #endif
  2050.     }
  2051.   UNGCPRO;
  2052.   return Qnil;
  2053. }
  2054. #endif /* S_IFLNK */
  2055.  
  2056. #ifdef VMS
  2057.  
  2058. DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name,
  2059.        2, 2, "sDefine logical name: \nsDefine logical name %s as: ",
  2060.   "Define the job-wide logical name NAME to have the value STRING.\n\
  2061. If STRING is nil or a null string, the logical name NAME is deleted.")
  2062.   (varname, string)
  2063.      Lisp_Object varname;
  2064.      Lisp_Object string;
  2065. {
  2066.   CHECK_STRING (varname, 0);
  2067.   if (NILP (string))
  2068.     delete_logical_name (XSTRING (varname)->data);
  2069.   else
  2070.     {
  2071.       CHECK_STRING (string, 1);
  2072.  
  2073.       if (XSTRING (string)->size == 0)
  2074.         delete_logical_name (XSTRING (varname)->data);
  2075.       else
  2076.         define_logical_name (XSTRING (varname)->data, XSTRING (string)->data);
  2077.     }
  2078.  
  2079.   return string;
  2080. }
  2081. #endif /* VMS */
  2082.  
  2083. #ifdef HPUX_NET
  2084.  
  2085. DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0,
  2086.        "Open a network connection to PATH using LOGIN as the login string.")
  2087.      (path, login)
  2088.      Lisp_Object path, login;
  2089. {
  2090.   int netresult;
  2091.   
  2092.   CHECK_STRING (path, 0);
  2093.   CHECK_STRING (login, 0);  
  2094.   
  2095.   netresult = netunam (XSTRING (path)->data, XSTRING (login)->data);
  2096.  
  2097.   if (netresult == -1)
  2098.     return Qnil;
  2099.   else
  2100.     return Qt;
  2101. }
  2102. #endif /* HPUX_NET */
  2103.  
  2104. DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p,
  2105.        1, 1, 0,
  2106.        "Return t if file FILENAME specifies an absolute path name.\n\
  2107. On Unix, this is a name starting with a `/' or a `~'.")
  2108.      (filename)
  2109.      Lisp_Object filename;
  2110. {
  2111.   unsigned char *ptr;
  2112.  
  2113.   CHECK_STRING (filename, 0);
  2114.   ptr = XSTRING (filename)->data;
  2115.   if (*ptr == PATH_SEPARATOR || *ptr == '~'
  2116. #ifdef VMS
  2117. /* ??? This criterion is probably wrong for '<'.  */
  2118.       || index (ptr, ':') || index (ptr, '<')
  2119.       || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']'))
  2120.       && ptr[1] != '.')
  2121. #endif /* VMS */
  2122.       )
  2123.     return Qt;
  2124.   else
  2125.     return Qnil;
  2126. }
  2127.  
  2128. DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0,
  2129.   "Return t if file FILENAME exists.  (This does not mean you can read it.)\n\
  2130. See also `file-readable-p' and `file-attributes'.")
  2131.   (filename)
  2132.      Lisp_Object filename;
  2133. {
  2134.   Lisp_Object abspath;
  2135.   Lisp_Object handler;
  2136.  
  2137.   CHECK_STRING (filename, 0);
  2138.   abspath = Fexpand_file_name (filename, Qnil);
  2139.  
  2140.   /* If the file name has special constructs in it,
  2141.      call the corresponding file handler.  */
  2142.   handler = Ffind_file_name_handler (abspath);
  2143.   if (!NILP (handler))
  2144.     return call2 (handler, Qfile_exists_p, abspath);
  2145.  
  2146.   return (access (XSTRING (abspath)->data, 0) >= 0) ? Qt : Qnil;
  2147. }
  2148.  
  2149. DEFUN ("file-executable-p", Ffile_executable_p, Sfile_executable_p, 1, 1, 0,
  2150.   "Return t if FILENAME can be executed by you.\n\
  2151. For a directory, this means you can access files in that directory.")
  2152.   (filename)
  2153.     Lisp_Object filename;
  2154.  
  2155. {
  2156.   Lisp_Object abspath;
  2157.   Lisp_Object handler;
  2158.  
  2159.   CHECK_STRING (filename, 0);
  2160.   abspath = Fexpand_file_name (filename, Qnil);
  2161.  
  2162.   /* If the file name has special constructs in it,
  2163.      call the corresponding file handler.  */
  2164.   handler = Ffind_file_name_handler (abspath);
  2165.   if (!NILP (handler))
  2166.     return call2 (handler, Qfile_executable_p, abspath);
  2167.  
  2168.   return (access (XSTRING (abspath)->data, 1) >= 0) ? Qt : Qnil;
  2169. }
  2170.  
  2171. DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
  2172.   "Return t if file FILENAME exists and you can read it.\n\
  2173. See also `file-exists-p' and `file-attributes'.")
  2174.   (filename)
  2175.      Lisp_Object filename;
  2176. {
  2177.   Lisp_Object abspath;
  2178.   Lisp_Object handler;
  2179.  
  2180.   CHECK_STRING (filename, 0);
  2181.   abspath = Fexpand_file_name (filename, Qnil);
  2182.  
  2183.   /* If the file name has special constructs in it,
  2184.      call the corresponding file handler.  */
  2185.   handler = Ffind_file_name_handler (abspath);
  2186.   if (!NILP (handler))
  2187.     return call2 (handler, Qfile_readable_p, abspath);
  2188.  
  2189.   return (access (XSTRING (abspath)->data, 4) >= 0) ? Qt : Qnil;
  2190. }
  2191.  
  2192. DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0,
  2193.   "If file FILENAME is the name of a symbolic link\n\
  2194. returns the name of the file to which it is linked.\n\
  2195. Otherwise returns NIL.")
  2196.   (filename)
  2197.      Lisp_Object filename;
  2198. {
  2199. #ifdef S_IFLNK
  2200.   char *buf;
  2201.   int bufsize;
  2202.   int valsize;
  2203.   Lisp_Object val;
  2204.   Lisp_Object handler;
  2205.  
  2206.   CHECK_STRING (filename, 0);
  2207.   filename = Fexpand_file_name (filename, Qnil);
  2208.  
  2209.   /* If the file name has special constructs in it,
  2210.      call the corresponding file handler.  */
  2211.   handler = Ffind_file_name_handler (filename);
  2212.   if (!NILP (handler))
  2213.     return call2 (handler, Qfile_symlink_p, filename);
  2214.  
  2215.   bufsize = 100;
  2216.   while (1)
  2217.     {
  2218.       buf = (char *) xmalloc (bufsize);
  2219.       bzero (buf, bufsize);
  2220.       valsize = readlink (XSTRING (filename)->data, buf, bufsize);
  2221.       if (valsize < bufsize) break;
  2222.       /* Buffer was not long enough */
  2223.       xfree (buf);
  2224.       bufsize *= 2;
  2225.     }
  2226.   if (valsize == -1)
  2227.     {
  2228.       xfree (buf);
  2229.       return Qnil;
  2230.     }
  2231.   val = make_string (buf, valsize);
  2232.   xfree (buf);
  2233.   return val;
  2234. #else /* not S_IFLNK */
  2235.   return Qnil;
  2236. #endif /* not S_IFLNK */
  2237. }
  2238.  
  2239. #ifdef SOLARIS_BROKEN_ACCESS
  2240. /* In Solaris 2.1, the readonly-ness of the filesystem is not
  2241.    considered by the access system call.  This is Sun's bug, but we
  2242.    still have to make Emacs work.  */
  2243.  
  2244. #include <sys/statvfs.h>
  2245.  
  2246. static int
  2247. ro_fsys (path)
  2248.     char *path;
  2249. {
  2250.     struct statvfs statvfsb;
  2251.  
  2252.     if (statvfs(path, &statvfsb))
  2253.       return 1;  /* error from statvfs, be conservative and say not wrtable */
  2254.     else
  2255.       /* Otherwise, fsys is ro if bit is set.  */
  2256.       return statvfsb.f_flag & ST_RDONLY;
  2257. }
  2258. #else
  2259. /* But on every other os, access has already done the right thing.  */
  2260. #define ro_fsys(path) 0
  2261. #endif
  2262.  
  2263. /* Having this before file-symlink-p mysteriously caused it to be forgotten
  2264.    on the RT/PC.  */
  2265. DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
  2266.   "Return t if file FILENAME can be written or created by you.")
  2267.   (filename)
  2268.      Lisp_Object filename;
  2269. {
  2270.   Lisp_Object abspath, dir;
  2271.   Lisp_Object handler;
  2272.  
  2273.   CHECK_STRING (filename, 0);
  2274.   abspath = Fexpand_file_name (filename, Qnil);
  2275.  
  2276.   /* If the file name has special constructs in it,
  2277.      call the corresponding file handler.  */
  2278.   handler = Ffind_file_name_handler (abspath);
  2279.   if (!NILP (handler))
  2280.     return call2 (handler, Qfile_writable_p, abspath);
  2281.  
  2282.   if (access (XSTRING (abspath)->data, 0) >= 0)
  2283.     return ((access (XSTRING (abspath)->data, 2) >= 0
  2284.          && ! ro_fsys ((char *) XSTRING (abspath)->data))
  2285.         ? Qt : Qnil);
  2286.   dir = Ffile_name_directory (abspath);
  2287. #ifdef VMS
  2288.   if (!NILP (dir))
  2289.     dir = Fdirectory_file_name (dir);
  2290. #endif /* VMS */
  2291.   return ((access (!NILP (dir) ? (char *) XSTRING (dir)->data : "", 2) >= 0
  2292.        && ! ro_fsys ((char *) XSTRING (dir)->data))
  2293.       ? Qt : Qnil);
  2294. }
  2295.  
  2296. DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
  2297.   "Return t if file FILENAME is the name of a directory as a file.\n\
  2298. A directory name spec may be given instead; then the value is t\n\
  2299. if the directory so specified exists and really is a directory.")
  2300.   (filename)
  2301.      Lisp_Object filename;
  2302. {
  2303.   register Lisp_Object abspath;
  2304.   struct stat st;
  2305.   Lisp_Object handler;
  2306.  
  2307.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  2308.  
  2309.   /* If the file name has special constructs in it,
  2310.      call the corresponding file handler.  */
  2311.   handler = Ffind_file_name_handler (abspath);
  2312.   if (!NILP (handler))
  2313.     return call2 (handler, Qfile_directory_p, abspath);
  2314.  
  2315.   if (stat (XSTRING (abspath)->data, &st) < 0)
  2316.     return Qnil;
  2317.   return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
  2318. }
  2319.  
  2320. DEFUN ("file-accessible-directory-p", Ffile_accessible_directory_p, Sfile_accessible_directory_p, 1, 1, 0,
  2321.   "Return t if file FILENAME is the name of a directory as a file,\n\
  2322. and files in that directory can be opened by you.  In order to use a\n\
  2323. directory as a buffer's current directory, this predicate must return true.\n\
  2324. A directory name spec may be given instead; then the value is t\n\
  2325. if the directory so specified exists and really is a readable and\n\
  2326. searchable directory.")
  2327.   (filename)
  2328.      Lisp_Object filename;
  2329. {
  2330.   Lisp_Object handler;
  2331.  
  2332.   /* If the file name has special constructs in it,
  2333.      call the corresponding file handler.  */
  2334.   handler = Ffind_file_name_handler (filename);
  2335.   if (!NILP (handler))
  2336.     return call2 (handler, Qfile_accessible_directory_p, filename);
  2337.  
  2338.   if (NILP (Ffile_directory_p (filename))
  2339.       || NILP (Ffile_executable_p (filename)))
  2340.     return Qnil;
  2341.   else
  2342.     return Qt;
  2343. }
  2344.  
  2345. DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0,
  2346.   "Return mode bits of FILE, as an integer.")
  2347.   (filename)
  2348.      Lisp_Object filename;
  2349. {
  2350.   Lisp_Object abspath;
  2351.   struct stat st;
  2352.   Lisp_Object handler;
  2353.  
  2354.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  2355.  
  2356.   /* If the file name has special constructs in it,
  2357.      call the corresponding file handler.  */
  2358.   handler = Ffind_file_name_handler (abspath);
  2359.   if (!NILP (handler))
  2360.     return call2 (handler, Qfile_modes, abspath);
  2361.  
  2362.   if (stat (XSTRING (abspath)->data, &st) < 0)
  2363.     return Qnil;
  2364.   return make_number (st.st_mode & 07777);
  2365. }
  2366.  
  2367. DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0,
  2368.   "Set mode bits of FILE to MODE (an integer).\n\
  2369. Only the 12 low bits of MODE are used.")
  2370.   (filename, mode)
  2371.      Lisp_Object filename, mode;
  2372. {
  2373.   Lisp_Object abspath;
  2374.   Lisp_Object handler;
  2375.  
  2376.   abspath = Fexpand_file_name (filename, current_buffer->directory);
  2377.   CHECK_NUMBER (mode, 1);
  2378.  
  2379.   /* If the file name has special constructs in it,
  2380.      call the corresponding file handler.  */
  2381.   handler = Ffind_file_name_handler (abspath);
  2382.   if (!NILP (handler))
  2383.     return call3 (handler, Qset_file_modes, abspath, mode);
  2384.  
  2385. #ifndef APOLLO
  2386.   if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  2387.     report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2388. #else /* APOLLO */
  2389.   if (!egetenv ("USE_DOMAIN_ACLS"))
  2390.     {
  2391.       struct stat st;
  2392.       struct timeval tvp[2];
  2393.  
  2394.       /* chmod on apollo also change the file's modtime; need to save the
  2395.      modtime and then restore it. */
  2396.       if (stat (XSTRING (abspath)->data, &st) < 0)
  2397.     {
  2398.       report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2399.       return (Qnil);
  2400.     }
  2401.  
  2402.       if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  2403.      report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  2404.  
  2405.       /* reset the old accessed and modified times.  */
  2406.       tvp[0].tv_sec = st.st_atime + 1; /* +1 due to an Apollo roundoff bug */
  2407.       tvp[0].tv_usec = 0;
  2408.       tvp[1].tv_sec = st.st_mtime + 1; /* +1 due to an Apollo roundoff bug */
  2409.       tvp[1].tv_usec = 0;
  2410.  
  2411.       if (utimes (XSTRING (abspath)->data, tvp) < 0)
  2412.      report_file_error ("Doing utimes", Fcons (abspath, Qnil));
  2413.     }
  2414. #endif /* APOLLO */
  2415.  
  2416.   return Qnil;
  2417. }
  2418.  
  2419. DEFUN ("set-default-file-modes", Fset_default_file_modes, Sset_default_file_modes, 1, 1, 0,
  2420.     "Set the file permission bits for newly created files.\n\
  2421. The argument MODE should be an integer; only the low 9 bits are used.\n\
  2422. This setting is inherited by subprocesses.")
  2423.   (mode)
  2424.      Lisp_Object mode;
  2425. {
  2426.   CHECK_NUMBER (mode, 0);
  2427.  
  2428. #ifndef WINDOWSNT  
  2429.   umask ((~ XINT (mode)) & 0777);
  2430. #endif /* !WINDOWSNT */
  2431.  
  2432.   return Qnil;
  2433. }
  2434.  
  2435. DEFUN ("default-file-modes", Fdefault_file_modes, Sdefault_file_modes, 0, 0, 0,
  2436.     "Return the default file protection for created files.\n\
  2437. The value is an integer.")
  2438.   ()
  2439. {
  2440.   int realmask;
  2441.   Lisp_Object value;
  2442.  
  2443. #ifdef WINDOWSNT
  2444.   realmask = 0;
  2445. #else /* !WINDOWSNT */
  2446.   realmask = umask (0);
  2447.   umask (realmask);
  2448. #endif /* !WINDOWSNT */
  2449.  
  2450.   XSET (value, Lisp_Int, (~ realmask) & 0777);
  2451.   return value;
  2452. }
  2453.  
  2454. #ifdef unix
  2455.  
  2456. DEFUN ("unix-sync", Funix_sync, Sunix_sync, 0, 0, "",
  2457.   "Tell Unix to finish all pending disk updates.")
  2458.   ()
  2459. {
  2460.   sync ();
  2461.   return Qnil;
  2462. }
  2463.  
  2464. #endif /* unix */
  2465.  
  2466. DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0,
  2467.   "Return t if file FILE1 is newer than file FILE2.\n\
  2468. If FILE1 does not exist, the answer is nil;\n\
  2469. otherwise, if FILE2 does not exist, the answer is t.")
  2470.   (file1, file2)
  2471.      Lisp_Object file1, file2;
  2472. {
  2473.   Lisp_Object abspath1, abspath2;
  2474.   struct stat st;
  2475.   int mtime1;
  2476.   Lisp_Object handler;
  2477.   struct gcpro gcpro1, gcpro2;
  2478.  
  2479.   CHECK_STRING (file1, 0);
  2480.   CHECK_STRING (file2, 0);
  2481.  
  2482.   abspath1 = Qnil;
  2483.   GCPRO2 (abspath1, file2);
  2484.   abspath1 = expand_and_dir_to_file (file1, current_buffer->directory);
  2485.   abspath2 = expand_and_dir_to_file (file2, current_buffer->directory);
  2486.   UNGCPRO;
  2487.  
  2488.   /* If the file name has special constructs in it,
  2489.      call the corresponding file handler.  */
  2490.   handler = Ffind_file_name_handler (abspath1);
  2491.   if (NILP (handler))
  2492.     handler = Ffind_file_name_handler (abspath2);
  2493.   if (!NILP (handler))
  2494.     return call3 (handler, Qfile_newer_than_file_p, abspath1, abspath2);
  2495.  
  2496.   if (stat (XSTRING (abspath1)->data, &st) < 0)
  2497.     return Qnil;
  2498.  
  2499.   mtime1 = st.st_mtime;
  2500.  
  2501.   if (stat (XSTRING (abspath2)->data, &st) < 0)
  2502.     return Qt;
  2503.  
  2504.   return (mtime1 > st.st_mtime) ? Qt : Qnil;
  2505. }
  2506.  
  2507. DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
  2508.   1, 4, 0,
  2509.   "Insert contents of file FILENAME after point.\n\
  2510. Returns list of absolute file name and length of data inserted.\n\
  2511. If second argument VISIT is non-nil, the buffer's visited filename\n\
  2512. and last save file modtime are set, and it is marked unmodified.\n\
  2513. If visiting and the file does not exist, visiting is completed\n\
  2514. before the error is signaled.\n\n\
  2515. The optional third and fourth arguments BEG and END\n\
  2516. specify what portion of the file to insert.\n\
  2517. If VISIT is non-nil, BEG and END must be nil.")
  2518.   (filename, visit, beg, end)
  2519.      Lisp_Object filename, visit, beg, end;
  2520. {
  2521.   struct stat st;
  2522.   register int fd;
  2523.   register int inserted = 0;
  2524.   register int how_much;
  2525.   int count = specpdl_ptr - specpdl;
  2526.   struct gcpro gcpro1;
  2527.   Lisp_Object handler, val;
  2528.   int total;
  2529.  
  2530.   val = Qnil;
  2531.  
  2532.   GCPRO1 (filename);
  2533.   if (!NILP (current_buffer->read_only))
  2534.     Fbarf_if_buffer_read_only();
  2535.  
  2536.   CHECK_STRING (filename, 0);
  2537.   filename = Fexpand_file_name (filename, Qnil);
  2538.  
  2539.   /* If the file name has special constructs in it,
  2540.      call the corresponding file handler.  */
  2541.   handler = Ffind_file_name_handler (filename);
  2542.   if (!NILP (handler))
  2543.     {
  2544.       val = call5 (handler, Qinsert_file_contents, filename, visit, beg, end);
  2545.       st.st_mtime = 0;
  2546.       goto handled;
  2547.     }
  2548.  
  2549.   fd = -1;
  2550.  
  2551. #ifndef APOLLO
  2552. #ifdef WINDOWSNT
  2553.   /*
  2554.    * We need to explicitly set the BINARY flag on NT to read
  2555.    * 8-bit characters and display them in a buffer.  Otherwise
  2556.    * we get EOF when encountering a Ctrl-Z.  BUT, this would
  2557.    * mean that we have to do our own CR-LF/LF processing, which
  2558.    * I don't want to fool with yet.    (voelker)
  2559.    */
  2560.   if (stat (XSTRING (filename)->data, &st) < 0
  2561.       || (fd = open (XSTRING (filename)->data, 0 /* _O_BINARY */)) < 0)
  2562. #else
  2563.   if (stat (XSTRING (filename)->data, &st) < 0
  2564.       || (fd = open (XSTRING (filename)->data, 0)) < 0)
  2565. #endif /* !WINDOWSNT */
  2566. #else
  2567.   if ((fd = open (XSTRING (filename)->data, 0)) < 0
  2568.       || fstat (fd, &st) < 0)
  2569. #endif /* not APOLLO */
  2570.     {
  2571.       if (fd >= 0) close (fd);
  2572.       if (NILP (visit))
  2573.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  2574.       st.st_mtime = -1;
  2575.       how_much = 0;
  2576.       goto notfound;
  2577.     }
  2578.  
  2579.   record_unwind_protect (close_file_unwind, make_number (fd));
  2580.  
  2581. #ifdef S_IFSOCK
  2582.   /* This code will need to be changed in order to work on named
  2583.      pipes, and it's probably just not worth it.  So we should at
  2584.      least signal an error.  */
  2585.   if ((st.st_mode & S_IFMT) == S_IFSOCK)
  2586.     Fsignal (Qfile_error,
  2587.          Fcons (build_string ("reading from named pipe"),
  2588.             Fcons (filename, Qnil)));
  2589. #endif
  2590.  
  2591.   /* Supposedly happens on VMS.  */
  2592.   if (st.st_size < 0)
  2593.     error ("File size is negative");
  2594.  
  2595.   if (!NILP (beg) || !NILP (end))
  2596.     if (!NILP (visit))
  2597.       error ("Attempt to visit less than an entire file");
  2598.  
  2599.   if (!NILP (beg))
  2600.     CHECK_NUMBER (beg, 0);
  2601.   else
  2602.     XFASTINT (beg) = 0;
  2603.  
  2604.   if (!NILP (end))
  2605.     CHECK_NUMBER (end, 0);
  2606.   else
  2607.     {
  2608.       XSETINT (end, st.st_size);
  2609.       if (XINT (end) != st.st_size)
  2610.     error ("maximum buffer size exceeded");
  2611.     }
  2612.  
  2613.   total = XINT (end) - XINT (beg);
  2614.  
  2615.   {
  2616.     register Lisp_Object temp;
  2617.  
  2618.     /* Make sure point-max won't overflow after this insertion.  */
  2619.     XSET (temp, Lisp_Int, total);
  2620.     if (total != XINT (temp))
  2621.       error ("maximum buffer size exceeded");
  2622.   }
  2623.  
  2624.   if (NILP (visit))
  2625.     prepare_to_modify_buffer (point, point);
  2626.  
  2627.   move_gap (point);
  2628.   if (GAP_SIZE < total)
  2629.     make_gap (total - GAP_SIZE);
  2630.  
  2631.   if (XINT (beg) != 0)
  2632.     {
  2633.       if (lseek (fd, XINT (beg), 0) < 0)
  2634.     report_file_error ("Setting file position", Fcons (filename, Qnil));
  2635.     }
  2636.  
  2637.   while (1)
  2638.     {
  2639. #ifdef WINDOWSNT
  2640.     /* try is reserved in Microsquish C */
  2641.       int trytry = min (total - inserted, 64 << 10);
  2642. #else /* !WINDOWSNT */
  2643.       int try = min (total - inserted, 64 << 10);
  2644. #endif /* !WINDOWSNT */
  2645.       int this;
  2646.  
  2647.       /* Allow quitting out of the actual I/O.  */
  2648.       immediate_quit = 1;
  2649.       QUIT;
  2650.       this = read (fd, &FETCH_CHAR (point + inserted - 1) + 1, 
  2651. #ifdef WINDOWSNT
  2652.            trytry);
  2653. #else /* !WINDOWSNT */
  2654.            try);
  2655. #endif /* !WINDOWSNT */
  2656.       immediate_quit = 0;
  2657.  
  2658.       if (this <= 0)
  2659.     {
  2660.       how_much = this;
  2661.       break;
  2662.     }
  2663.  
  2664.       GPT += this;
  2665.       GAP_SIZE -= this;
  2666.       ZV += this;
  2667.       Z += this;
  2668.       inserted += this;
  2669.     }
  2670.  
  2671.   if (inserted > 0)
  2672.     {
  2673.       record_insert (point, inserted);
  2674.  
  2675.       /* Only defined if Emacs is compiled with USE_TEXT_PROPERTIES */
  2676.       offset_intervals (current_buffer, point, inserted);
  2677.       MODIFF++;
  2678.     }
  2679.  
  2680.   close (fd);
  2681.  
  2682.   /* Discard the unwind protect */
  2683.   specpdl_ptr = specpdl + count;
  2684.  
  2685.   if (how_much < 0)
  2686.     error ("IO error reading %s: %s",
  2687.        XSTRING (filename)->data, err_str (errno));
  2688.  
  2689.  notfound:
  2690.  handled:
  2691.  
  2692.   if (!NILP (visit))
  2693.     {
  2694.       current_buffer->undo_list = Qnil;
  2695. #ifdef APOLLO
  2696.       stat (XSTRING (filename)->data, &st);
  2697. #endif
  2698.       current_buffer->modtime = st.st_mtime;
  2699.       current_buffer->save_modified = MODIFF;
  2700.       current_buffer->auto_save_modified = MODIFF;
  2701.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2702. #ifdef CLASH_DETECTION
  2703.       if (NILP (handler))
  2704.     {
  2705.       if (!NILP (current_buffer->filename))
  2706.         unlock_file (current_buffer->filename);
  2707.       unlock_file (filename);
  2708.     }
  2709. #endif /* CLASH_DETECTION */
  2710.       current_buffer->filename = filename;
  2711.       /* If visiting nonexistent file, return nil.  */
  2712.       if (current_buffer->modtime == -1)
  2713.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  2714.     }
  2715.  
  2716.   signal_after_change (point, 0, inserted);
  2717.   
  2718.   if (!NILP (val))
  2719.     RETURN_UNGCPRO (val);
  2720.   RETURN_UNGCPRO (Fcons (filename,
  2721.              Fcons (make_number (inserted),
  2722.                 Qnil)));
  2723. }
  2724.  
  2725. DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 5,
  2726.   "r\nFWrite region to file: ",
  2727.   "Write current region into specified file.\n\
  2728. When called from a program, takes three arguments:\n\
  2729. START, END and FILENAME.  START and END are buffer positions.\n\
  2730. Optional fourth argument APPEND if non-nil means\n\
  2731.   append to existing file contents (if any).\n\
  2732. Optional fifth argument VISIT if t means\n\
  2733.   set the last-save-file-modtime of buffer to this file's modtime\n\
  2734.   and mark buffer not modified.\n\
  2735. If VISIT is a string, it is a second file name;\n\
  2736.   the output goes to FILENAME, but the buffer is marked as visiting VISIT.\n\
  2737.   VISIT is also the file name to lock and unlock for clash detection.\n\
  2738. If VISIT is neither t nor nil nor a string,\n\
  2739.   that means do not print the \"Wrote file\" message.\n\
  2740. Kludgy feature: if START is a string, then that string is written\n\
  2741. to the file, instead of any buffer contents, and END is ignored.")
  2742.   (start, end, filename, append, visit)
  2743.      Lisp_Object start, end, filename, append, visit;
  2744. {
  2745.   register int desc;
  2746.   int failure;
  2747.   int save_errno;
  2748.   unsigned char *fn;
  2749.   struct stat st;
  2750.   int tem;
  2751.   int count = specpdl_ptr - specpdl;
  2752. #ifdef VMS
  2753.   unsigned char *fname = 0;    /* If non-0, original filename (must rename) */
  2754. #endif /* VMS */
  2755.   Lisp_Object handler;
  2756.   Lisp_Object visit_file;
  2757.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  2758.  
  2759.   /* Special kludge to simplify auto-saving */
  2760.   if (NILP (start))
  2761.     {
  2762.       XFASTINT (start) = BEG;
  2763.       XFASTINT (end) = Z;
  2764.     }
  2765.   else if (XTYPE (start) != Lisp_String)
  2766.     validate_region (&start, &end);
  2767.  
  2768.   filename = Fexpand_file_name (filename, Qnil);
  2769.   if (XTYPE (visit) == Lisp_String)
  2770.     visit_file = Fexpand_file_name (visit, Qnil);
  2771.   else
  2772.     visit_file = filename;
  2773.  
  2774.   GCPRO4 (start, filename, visit, visit_file);
  2775.  
  2776.   /* If the file name has special constructs in it,
  2777.      call the corresponding file handler.  */
  2778.   handler = Ffind_file_name_handler (filename);
  2779.  
  2780.   if (!NILP (handler))
  2781.     {
  2782.       Lisp_Object val;
  2783.       val = call6 (handler, Qwrite_region, start, end,
  2784.            filename, append, visit);
  2785.  
  2786.       /* Do this before reporting IO error
  2787.      to avoid a "file has changed on disk" warning on
  2788.      next attempt to save.  */
  2789.       if (EQ (visit, Qt) || XTYPE (visit) == Lisp_String)
  2790.     {
  2791.       current_buffer->modtime = 0;
  2792.       current_buffer->save_modified = MODIFF;
  2793.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2794.       current_buffer->filename = visit_file;
  2795.     }
  2796.       UNGCPRO;
  2797.       return val;
  2798.     }
  2799.  
  2800. #ifdef CLASH_DETECTION
  2801.   if (!auto_saving)
  2802.     lock_file (visit_file);
  2803. #endif /* CLASH_DETECTION */
  2804.  
  2805.   fn = XSTRING (filename)->data;
  2806.   desc = -1;
  2807.   if (!NILP (append))
  2808.     desc = open (fn, O_WRONLY);
  2809.  
  2810.   if (desc < 0)
  2811. #ifdef VMS
  2812.     if (auto_saving)    /* Overwrite any previous version of autosave file */
  2813.       {
  2814.     vms_truncate (fn);    /* if fn exists, truncate to zero length */
  2815.     desc = open (fn, O_RDWR);
  2816.     if (desc < 0)
  2817.       desc = creat_copy_attrs (XTYPE (current_buffer->filename) == Lisp_String
  2818.                    ? XSTRING (current_buffer->filename)->data : 0,
  2819.                    fn);
  2820.       }
  2821.     else        /* Write to temporary name and rename if no errors */
  2822.       {
  2823.     Lisp_Object temp_name;
  2824.     temp_name = Ffile_name_directory (filename);
  2825.  
  2826.     if (!NILP (temp_name))
  2827.       {
  2828.         temp_name = Fmake_temp_name (concat2 (temp_name,
  2829.                           build_string ("$$SAVE$$")));
  2830.         fname = XSTRING (filename)->data;
  2831.         fn = XSTRING (temp_name)->data;
  2832.         desc = creat_copy_attrs (fname, fn);
  2833.         if (desc < 0)
  2834.           {
  2835.         /* If we can't open the temporary file, try creating a new
  2836.            version of the original file.  VMS "creat" creates a
  2837.            new version rather than truncating an existing file. */
  2838.         fn = fname;
  2839.         fname = 0;
  2840.         desc = creat (fn, 0666);
  2841. #if 0 /* This can clobber an existing file and fail to replace it,
  2842.      if the user runs out of space.  */
  2843.         if (desc < 0)
  2844.           {
  2845.             /* We can't make a new version;
  2846.                try to truncate and rewrite existing version if any.  */
  2847.             vms_truncate (fn);
  2848.             desc = open (fn, O_RDWR);
  2849.           }
  2850. #endif
  2851.           }
  2852.       }
  2853.     else
  2854.       desc = creat (fn, 0666);
  2855.       }
  2856. #else /* not VMS */
  2857.   desc = creat (fn, auto_saving ? auto_save_mode_bits : 0666);
  2858. #endif /* not VMS */
  2859.  
  2860.   UNGCPRO;
  2861.  
  2862.   if (desc < 0)
  2863.     {
  2864. #ifdef CLASH_DETECTION
  2865.       save_errno = errno;
  2866.       if (!auto_saving) unlock_file (visit_file);
  2867.       errno = save_errno;
  2868. #endif /* CLASH_DETECTION */
  2869.       report_file_error ("Opening output file", Fcons (filename, Qnil));
  2870.     }
  2871.  
  2872.   record_unwind_protect (close_file_unwind, make_number (desc));
  2873.  
  2874.   if (!NILP (append))
  2875.     if (lseek (desc, 0, 2) < 0)
  2876.       {
  2877. #ifdef CLASH_DETECTION
  2878.     if (!auto_saving) unlock_file (visit_file);
  2879. #endif /* CLASH_DETECTION */
  2880.     report_file_error ("Lseek error", Fcons (filename, Qnil));
  2881.       }
  2882.  
  2883. #ifdef VMS
  2884. /*
  2885.  * Kludge Warning: The VMS C RTL likes to insert carriage returns
  2886.  * if we do writes that don't end with a carriage return. Furthermore
  2887.  * it cannot handle writes of more then 16K. The modified
  2888.  * version of "sys_write" in SYSDEP.C (see comment there) copes with
  2889.  * this EXCEPT for the last record (iff it doesn't end with a carriage
  2890.  * return). This implies that if your buffer doesn't end with a carriage
  2891.  * return, you get one free... tough. However it also means that if
  2892.  * we make two calls to sys_write (a la the following code) you can
  2893.  * get one at the gap as well. The easiest way to fix this (honest)
  2894.  * is to move the gap to the next newline (or the end of the buffer).
  2895.  * Thus this change.
  2896.  *
  2897.  * Yech!
  2898.  */
  2899.   if (GPT > BEG && GPT_ADDR[-1] != '\n')
  2900.     move_gap (find_next_newline (GPT, 1));
  2901. #endif
  2902.  
  2903.   failure = 0;
  2904.   immediate_quit = 1;
  2905.  
  2906.   if (XTYPE (start) == Lisp_String)
  2907.     {
  2908.       failure = 0 > e_write (desc, XSTRING (start)->data,
  2909.                  XSTRING (start)->size);
  2910.       save_errno = errno;
  2911.     }
  2912.   else if (XINT (start) != XINT (end))
  2913.     {
  2914.       if (XINT (start) < GPT)
  2915.     {
  2916.       register int end1 = XINT (end);
  2917.       tem = XINT (start);
  2918.       failure = 0 > e_write (desc, &FETCH_CHAR (tem),
  2919.                  min (GPT, end1) - tem);
  2920.       save_errno = errno;
  2921.     }
  2922.  
  2923.       if (XINT (end) > GPT && !failure)
  2924.     {
  2925.       tem = XINT (start);
  2926.       tem = max (tem, GPT);
  2927.       failure = 0 > e_write (desc, &FETCH_CHAR (tem), XINT (end) - tem);
  2928.       save_errno = errno;
  2929.     }
  2930.     }
  2931.  
  2932.   immediate_quit = 0;
  2933.  
  2934. #ifdef HAVE_FSYNC
  2935.   /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
  2936.      Disk full in NFS may be reported here.  */
  2937.   /* mib says that closing the file will try to write as fast as NFS can do
  2938.      it, and that means the fsync here is not crucial for autosave files.  */
  2939.   if (!auto_saving && fsync (desc) < 0)
  2940.     failure = 1, save_errno = errno;
  2941. #endif
  2942.  
  2943.   /* Spurious "file has changed on disk" warnings have been 
  2944.      observed on Suns as well.
  2945.      It seems that `close' can change the modtime, under nfs.
  2946.  
  2947.      (This has supposedly been fixed in Sunos 4,
  2948.      but who knows about all the other machines with NFS?)  */
  2949. #if 0
  2950.  
  2951.   /* On VMS and APOLLO, must do the stat after the close
  2952.      since closing changes the modtime.  */
  2953. #ifndef VMS
  2954. #ifndef APOLLO
  2955.   /* Recall that #if defined does not work on VMS.  */
  2956. #define FOO
  2957.   fstat (desc, &st);
  2958. #endif
  2959. #endif
  2960. #endif
  2961.  
  2962.   /* NFS can report a write failure now.  */
  2963.   if (close (desc) < 0)
  2964.     failure = 1, save_errno = errno;
  2965.  
  2966. #ifdef VMS
  2967.   /* If we wrote to a temporary name and had no errors, rename to real name. */
  2968.   if (fname)
  2969.     {
  2970.       if (!failure)
  2971.     failure = (rename (fn, fname) != 0), save_errno = errno;
  2972.       fn = fname;
  2973.     }
  2974. #endif /* VMS */
  2975.  
  2976. #ifndef FOO
  2977.   stat (fn, &st);
  2978. #endif
  2979.   /* Discard the unwind protect */
  2980.   specpdl_ptr = specpdl + count;
  2981.  
  2982. #ifdef CLASH_DETECTION
  2983.   if (!auto_saving)
  2984.     unlock_file (visit_file);
  2985. #endif /* CLASH_DETECTION */
  2986.  
  2987.   /* Do this before reporting IO error
  2988.      to avoid a "file has changed on disk" warning on
  2989.      next attempt to save.  */
  2990.   if (EQ (visit, Qt) || XTYPE (visit) == Lisp_String)
  2991.     current_buffer->modtime = st.st_mtime;
  2992.  
  2993.   if (failure)
  2994.     error ("IO error writing %s: %s", fn, err_str (save_errno));
  2995.  
  2996.   if (EQ (visit, Qt) || XTYPE (visit) == Lisp_String)
  2997.     {
  2998.       current_buffer->save_modified = MODIFF;
  2999.       XFASTINT (current_buffer->save_length) = Z - BEG;
  3000.       current_buffer->filename = visit_file;
  3001.     }
  3002.   else if (!NILP (visit))
  3003.     return Qnil;
  3004.  
  3005.   if (!auto_saving)
  3006.     message ("Wrote %s", XSTRING (visit_file)->data);
  3007.  
  3008.   return Qnil;
  3009. }
  3010.  
  3011. int
  3012. e_write (desc, addr, len)
  3013.      int desc;
  3014.      register char *addr;
  3015.      register int len;
  3016. {
  3017.   char buf[16 * 1024];
  3018.   register char *p, *end;
  3019.  
  3020.   if (!EQ (current_buffer->selective_display, Qt))
  3021.     return write (desc, addr, len) - len;
  3022.   else
  3023.     {
  3024.       p = buf;
  3025.       end = p + sizeof buf;
  3026.       while (len--)
  3027.     {
  3028.       if (p == end)
  3029.         {
  3030.           if (write (desc, buf, sizeof buf) != sizeof buf)
  3031.         return -1;
  3032.           p = buf;
  3033.         }
  3034.       *p = *addr++;
  3035.       if (*p++ == '\015')
  3036.         p[-1] = '\n';
  3037.     }
  3038.       if (p != buf)
  3039.     if (write (desc, buf, p - buf) != p - buf)
  3040.       return -1;
  3041.     }
  3042.   return 0;
  3043. }
  3044.  
  3045. DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime,
  3046.   Sverify_visited_file_modtime, 1, 1, 0,
  3047.   "Return t if last mod time of BUF's visited file matches what BUF records.\n\
  3048. This means that the file has not been changed since it was visited or saved.")
  3049.   (buf)
  3050.      Lisp_Object buf;
  3051. {
  3052.   struct buffer *b;
  3053.   struct stat st;
  3054.   Lisp_Object handler;
  3055.  
  3056.   CHECK_BUFFER (buf, 0);
  3057.   b = XBUFFER (buf);
  3058.  
  3059.   if (XTYPE (b->filename) != Lisp_String) return Qt;
  3060.   if (b->modtime == 0) return Qt;
  3061.  
  3062.   /* If the file name has special constructs in it,
  3063.      call the corresponding file handler.  */
  3064.   handler = Ffind_file_name_handler (b->filename);
  3065.   if (!NILP (handler))
  3066.     return call2 (handler, Qverify_visited_file_modtime, buf);
  3067.  
  3068.   if (stat (XSTRING (b->filename)->data, &st) < 0)
  3069.     {
  3070.       /* If the file doesn't exist now and didn't exist before,
  3071.      we say that it isn't modified, provided the error is a tame one.  */
  3072.       if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
  3073.     st.st_mtime = -1;
  3074.       else
  3075.     st.st_mtime = 0;
  3076.     }
  3077.   if (st.st_mtime == b->modtime
  3078.       /* If both are positive, accept them if they are off by one second.  */
  3079.       || (st.st_mtime > 0 && b->modtime > 0
  3080.       && (st.st_mtime == b->modtime + 1
  3081.           || st.st_mtime == b->modtime - 1)))
  3082.     return Qt;
  3083.   return Qnil;
  3084. }
  3085.  
  3086. DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
  3087.   Sclear_visited_file_modtime, 0, 0, 0,
  3088.   "Clear out records of last mod time of visited file.\n\
  3089. Next attempt to save will certainly not complain of a discrepancy.")
  3090.   ()
  3091. {
  3092.   current_buffer->modtime = 0;
  3093.   return Qnil;
  3094. }
  3095.  
  3096. DEFUN ("visited-file-modtime", Fvisited_file_modtime,
  3097.   Svisited_file_modtime, 0, 0, 0,
  3098.   "Return the current buffer's recorded visited file modification time.\n\
  3099. The value is a list of the form (HIGH . LOW), like the time values\n\
  3100. that `file-attributes' returns.")
  3101.   ()
  3102. {
  3103.   return long_to_cons (current_buffer->modtime);
  3104. }
  3105.  
  3106. DEFUN ("set-visited-file-modtime", Fset_visited_file_modtime,
  3107.   Sset_visited_file_modtime, 0, 1, 0,
  3108.   "Update buffer's recorded modification time from the visited file's time.\n\
  3109. Useful if the buffer was not read from the file normally\n\
  3110. or if the file itself has been changed for some known benign reason.\n\
  3111. An argument specifies the modification time value to use\n\
  3112. \(instead of that of the visited file), in the form of a list\n\
  3113. \(HIGH . LOW) or (HIGH LOW).")
  3114.   (time_list)
  3115.      Lisp_Object time_list;
  3116. {
  3117.   if (!NILP (time_list))
  3118.     current_buffer->modtime = cons_to_long (time_list);
  3119.   else
  3120.     {
  3121.       register Lisp_Object filename;
  3122.       struct stat st;
  3123.       Lisp_Object handler;
  3124.  
  3125.       filename = Fexpand_file_name (current_buffer->filename, Qnil);
  3126.  
  3127.       /* If the file name has special constructs in it,
  3128.      call the corresponding file handler.  */
  3129.       handler = Ffind_file_name_handler (filename);
  3130.       if (!NILP (handler))
  3131.     /* The handler can find the file name the same way we did.  */
  3132.     return call2 (handler, Qset_visited_file_modtime, Qnil);
  3133.       else if (stat (XSTRING (filename)->data, &st) >= 0)
  3134.     current_buffer->modtime = st.st_mtime;
  3135.     }
  3136.  
  3137.   return Qnil;
  3138. }
  3139.  
  3140. Lisp_Object
  3141. auto_save_error (dummy)
  3142.     /* Need a parameter */
  3143.     Lisp_Object dummy;
  3144. {
  3145.   unsigned char *name = XSTRING (current_buffer->name)->data;
  3146.  
  3147.   ring_bell ();
  3148.   message ("Autosaving...error for %s", name);
  3149.   Fsleep_for (make_number (1), Qnil);
  3150.   message ("Autosaving...error!for %s", name);
  3151.   Fsleep_for (make_number (1), Qnil);
  3152.   message ("Autosaving...error for %s", name);
  3153.   Fsleep_for (make_number (1), Qnil);
  3154.   return Qnil;
  3155. }
  3156.  
  3157. Lisp_Object
  3158. auto_save_1 _P_((void))
  3159. {
  3160.   struct stat st;
  3161.  
  3162.   /* Get visited file's mode to become the auto save file's mode.  */
  3163.   if (stat (XSTRING (current_buffer->filename)->data, &st) >= 0)
  3164.     /* But make sure we can overwrite it later!  */
  3165.     auto_save_mode_bits = st.st_mode | 0600;
  3166.   else
  3167.     auto_save_mode_bits = 0666;
  3168.  
  3169.   return
  3170.     Fwrite_region (Qnil, Qnil,
  3171.            current_buffer->auto_save_file_name,
  3172.            Qnil, Qlambda);
  3173. }
  3174.  
  3175. DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 2, "",
  3176.   "Auto-save all buffers that need it.\n\
  3177. This is all buffers that have auto-saving enabled\n\
  3178. and are changed since last auto-saved.\n\
  3179. Auto-saving writes the buffer into a file\n\
  3180. so that your editing is not lost if the system crashes.\n\
  3181. This file is not the file you visited; that changes only when you save.\n\n\
  3182. Non-nil first argument means do not print any message if successful.\n\
  3183. Non-nil second argument means save only current buffer.")
  3184.   (no_message, current_only)
  3185.      Lisp_Object no_message, current_only;
  3186. {
  3187.   struct buffer *old = current_buffer, *b;
  3188.   Lisp_Object tail, buf;
  3189.   int auto_saved = 0;
  3190.   char *omessage = echo_area_glyphs;
  3191.   extern int minibuf_level;
  3192.   int do_handled_files;
  3193.  
  3194.   /* No GCPRO needed, because (when it matters) all Lisp_Object variables
  3195.      point to non-strings reached from Vbuffer_alist.  */
  3196.  
  3197.   auto_saving = 1;
  3198.   if (minibuf_level)
  3199.     no_message = Qt;
  3200.  
  3201.   /* Vrun_hooks is nil before emacs is dumped, and inc-vers.el will
  3202.      eventually call do-auto-save, so don't err here in that case. */
  3203.   if (!NILP (Vrun_hooks))
  3204.     call1 (Vrun_hooks, intern ("auto-save-hook"));
  3205.  
  3206.   /* First, save all files which don't have handlers.  If Emacs is
  3207.      crashing, the handlers may tweak what is causing Emacs to crash
  3208.      in the first place, and it would be a shame if Emacs failed to
  3209.      autosave perfectly ordinary files because it couldn't handle some
  3210.      ange-ftp'd file.  */
  3211.   for (do_handled_files = 0; do_handled_files < 2; do_handled_files++)
  3212.     for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  3213.      tail = XCONS (tail)->cdr)
  3214.       {
  3215.     buf = XCONS (XCONS (tail)->car)->cdr;
  3216.     b = XBUFFER (buf);
  3217.  
  3218.     if (!NILP (current_only)
  3219.         && b != current_buffer)
  3220.       continue;
  3221.       
  3222.     /* Check for auto save enabled
  3223.        and file changed since last auto save
  3224.        and file changed since last real save.  */
  3225.     if (XTYPE (b->auto_save_file_name) == Lisp_String
  3226.         && b->save_modified < BUF_MODIFF (b)
  3227.         && b->auto_save_modified < BUF_MODIFF (b)
  3228.         && (do_handled_files
  3229.         || NILP (Ffind_file_name_handler (b->auto_save_file_name))))
  3230.       {
  3231.         if ((XFASTINT (b->save_length) * 10
  3232.          > (BUF_Z (b) - BUF_BEG (b)) * 13)
  3233.         /* A short file is likely to change a large fraction;
  3234.            spare the user annoying messages.  */
  3235.         && XFASTINT (b->save_length) > 5000
  3236.         /* These messages are frequent and annoying for `*mail*'.  */
  3237.         && !EQ (b->filename, Qnil)
  3238.         && NILP (no_message))
  3239.           {
  3240.         /* It has shrunk too much; turn off auto-saving here.  */
  3241.         message ("Buffer %s has shrunk a lot; auto save turned off there",
  3242.              XSTRING (b->name)->data);
  3243.         /* User can reenable saving with M-x auto-save.  */
  3244.         b->auto_save_file_name = Qnil;
  3245.         /* Prevent warning from repeating if user does so.  */
  3246.         XFASTINT (b->save_length) = 0;
  3247.         Fsleep_for (make_number (1), Qnil);
  3248.         continue;
  3249.           }
  3250.         set_buffer_internal (b);
  3251.         if (!auto_saved && NILP (no_message))
  3252.           message1 ("Auto-saving...");
  3253.         internal_condition_case (auto_save_1, Qt, auto_save_error);
  3254.         auto_saved++;
  3255.         b->auto_save_modified = BUF_MODIFF (b);
  3256.         XFASTINT (current_buffer->save_length) = Z - BEG;
  3257.         set_buffer_internal (old);
  3258.       }
  3259.       }
  3260.  
  3261.   /* Prevent another auto save till enough input events come in.  */
  3262.   record_auto_save ();
  3263.  
  3264.   if (auto_saved && NILP (no_message))
  3265.     message1 (omessage ? omessage : "Auto-saving...done");
  3266.  
  3267.   auto_saving = 0;
  3268.   return Qnil;
  3269. }
  3270.  
  3271. DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved,
  3272.   Sset_buffer_auto_saved, 0, 0, 0,
  3273.   "Mark current buffer as auto-saved with its current text.\n\
  3274. No auto-save file will be written until the buffer changes again.")
  3275.   ()
  3276. {
  3277.   current_buffer->auto_save_modified = MODIFF;
  3278.   XFASTINT (current_buffer->save_length) = Z - BEG;
  3279.   return Qnil;
  3280. }
  3281.  
  3282. DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p,
  3283.   0, 0, 0,
  3284.   "Return t if buffer has been auto-saved since last read in or saved.")
  3285.   ()
  3286. {
  3287.   return (current_buffer->save_modified < current_buffer->auto_save_modified) ? Qt : Qnil;
  3288. }
  3289.  
  3290. /* Reading and completing file names */
  3291. DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal,
  3292.   3, 3, 0,
  3293.   "Internal subroutine for read-file-name.  Do not call this.")
  3294.   (string, dir, action)
  3295.      Lisp_Object string, dir, action;
  3296.   /* action is nil for complete, t for return list of completions,
  3297.      lambda for verify final value */
  3298. {
  3299.   Lisp_Object name, specdir, realdir, val, orig_string;
  3300.   int changed;
  3301.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  3302.  
  3303.   realdir = dir;
  3304.   name = string;
  3305.   orig_string = Qnil;
  3306.   specdir = Qnil;
  3307.   changed = 0;
  3308.   /* No need to protect ACTION--we only compare it with t and nil.  */
  3309.   GCPRO4 (string, realdir, name, specdir);
  3310.  
  3311.   if (XSTRING (string)->size == 0)
  3312.     {
  3313.       if (EQ (action, Qlambda))
  3314.     {
  3315.       UNGCPRO;
  3316.       return Qnil;
  3317.     }
  3318.     }
  3319.   else
  3320.     {
  3321.       orig_string = string;
  3322.       string = Fsubstitute_in_file_name (string);
  3323.       changed = NILP (Fstring_equal (string, orig_string));
  3324.       name = Ffile_name_nondirectory (string);
  3325.       val = Ffile_name_directory (string);
  3326.       if (! NILP (val))
  3327.     realdir = Fexpand_file_name (val, realdir);
  3328.     }
  3329.  
  3330.   if (NILP (action))
  3331.     {
  3332.       specdir = Ffile_name_directory (string);
  3333.       val = Ffile_name_completion (name, realdir);
  3334.       UNGCPRO;
  3335.       if (XTYPE (val) != Lisp_String)
  3336.     {
  3337.       if (changed)
  3338.         return string;
  3339.       return val;
  3340.     }
  3341.  
  3342.       if (!NILP (specdir))
  3343.     val = concat2 (specdir, val);
  3344. #ifndef VMS
  3345.       {
  3346.     register unsigned char *old, *new;
  3347.     register int n;
  3348.     int osize, count;
  3349.  
  3350.     osize = XSTRING (val)->size;
  3351.     /* Quote "$" as "$$" to get it past substitute-in-file-name */
  3352.     for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--)
  3353.       if (*old++ == '$') count++;
  3354.     if (count > 0)
  3355.       {
  3356.         old = XSTRING (val)->data;
  3357.         val = Fmake_string (make_number (osize + count), make_number (0));
  3358.         new = XSTRING (val)->data;
  3359.         for (n = osize; n > 0; n--)
  3360.           if (*old != '$')
  3361.         *new++ = *old++;
  3362.           else
  3363.         {
  3364.           *new++ = '$';
  3365.           *new++ = '$';
  3366.           old++;
  3367.         }
  3368.       }
  3369.       }
  3370. #endif /* Not VMS */
  3371.       return val;
  3372.     }
  3373.   UNGCPRO;
  3374.  
  3375.   if (EQ (action, Qt))
  3376.     return Ffile_name_all_completions (name, realdir);
  3377.   /* Only other case actually used is ACTION = lambda */
  3378. #ifdef VMS
  3379.   /* Supposedly this helps commands such as `cd' that read directory names,
  3380.      but can someone explain how it helps them? -- RMS */
  3381.   if (XSTRING (name)->size == 0)
  3382.     return Qt;
  3383. #endif /* VMS */
  3384.   return Ffile_exists_p (string);
  3385. }
  3386.  
  3387. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0,
  3388.   "Read file name, prompting with PROMPT and completing in directory DIR.\n\
  3389. Value is not expanded---you must call `expand-file-name' yourself.\n\
  3390. Default name to DEFAULT if user enters a null string.\n\
  3391.  (If DEFAULT is omitted, the visited file name is used.)\n\
  3392. Fourth arg MUSTMATCH non-nil means require existing file's name.\n\
  3393.  Non-nil and non-t means also require confirmation after completion.\n\
  3394. Fifth arg INITIAL specifies text to start with.\n\
  3395. DIR defaults to current buffer's directory default.")
  3396.   (prompt, dir, defalt, mustmatch, initial)
  3397.      Lisp_Object prompt, dir, defalt, mustmatch, initial;
  3398. {
  3399.   Lisp_Object val, insdef, insdef1, tem;
  3400.   struct gcpro gcpro1, gcpro2;
  3401.   register char *homedir;
  3402. #ifdef VMS
  3403.   int count;
  3404. #endif
  3405.  
  3406.   if (NILP (dir))
  3407.     dir = current_buffer->directory;
  3408.   if (NILP (defalt))
  3409.     defalt = current_buffer->filename;
  3410.  
  3411.   /* If dir starts with user's homedir, change that to ~. */
  3412.   homedir = (char *) egetenv ("HOME");
  3413.   if (homedir != 0
  3414.       && XTYPE (dir) == Lisp_String
  3415.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  3416.       && XSTRING (dir)->data[strlen (homedir)] == PATH_SEPARATOR)
  3417.     {
  3418.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  3419.              XSTRING (dir)->size - strlen (homedir) + 1);
  3420.       XSTRING (dir)->data[0] = '~';
  3421.     }
  3422.  
  3423.   if (insert_default_directory)
  3424.     {
  3425.       insdef = dir;
  3426.       insdef1 = dir;
  3427.       if (!NILP (initial))
  3428.     {
  3429.       Lisp_Object args[2], pos;
  3430.  
  3431.       args[0] = insdef;
  3432.       args[1] = initial;
  3433.       insdef = Fconcat (2, args);
  3434.       pos = make_number (XSTRING (dir)->size);
  3435.       insdef1 = Fcons (insdef, pos);
  3436.     }
  3437.     }
  3438.   else
  3439.     insdef = Qnil, insdef1 = Qnil;
  3440.  
  3441. #ifdef VMS
  3442.   count = specpdl_ptr - specpdl;
  3443.   specbind (intern ("completion-ignore-case"), Qt);
  3444. #endif
  3445.  
  3446.   GCPRO2 (insdef, defalt);
  3447.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  3448.               dir, mustmatch, insdef1,
  3449.               Qfile_name_history);
  3450.  
  3451. #ifdef VMS
  3452.   unbind_to (count, Qnil);
  3453. #endif
  3454.  
  3455.   UNGCPRO;
  3456.   if (NILP (val))
  3457.     error ("No file name specified");
  3458.   tem = Fstring_equal (val, insdef);
  3459.   if (!NILP (tem) && !NILP (defalt))
  3460.     return defalt;
  3461.   if (XSTRING (val)->size == 0 && NILP (insdef))
  3462.     return defalt;
  3463.   return Fsubstitute_in_file_name (val);
  3464. }
  3465.  
  3466. #if 0                /* Old version */
  3467. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 5, 0,
  3468.   "Read file name, prompting with PROMPT and completing in directory DIR.\n\
  3469. Value is not expanded---you must call `expand-file-name' yourself.\n\
  3470. Default name to DEFAULT if user enters a null string.\n\
  3471.  (If DEFAULT is omitted, the visited file name is used.)\n\
  3472. Fourth arg MUSTMATCH non-nil means require existing file's name.\n\
  3473.  Non-nil and non-t means also require confirmation after completion.\n\
  3474. Fifth arg INITIAL specifies text to start with.\n\
  3475. DIR defaults to current buffer's directory default.")
  3476.   (prompt, dir, defalt, mustmatch, initial)
  3477.      Lisp_Object prompt, dir, defalt, mustmatch, initial;
  3478. {
  3479.   Lisp_Object val, insdef, tem;
  3480.   struct gcpro gcpro1, gcpro2;
  3481.   register char *homedir;
  3482.   int count;
  3483.  
  3484.   if (NILP (dir))
  3485.     dir = current_buffer->directory;
  3486.   if (NILP (defalt))
  3487.     defalt = current_buffer->filename;
  3488.  
  3489.   /* If dir starts with user's homedir, change that to ~. */
  3490.   homedir = (char *) egetenv ("HOME");
  3491.   if (homedir != 0
  3492.       && XTYPE (dir) == Lisp_String
  3493.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  3494.       && IS_ANY_SEPARATOR(XSTRING (dir)->data[strlen (homedir)]))
  3495.     {
  3496.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  3497.              XSTRING (dir)->size - strlen (homedir) + 1);
  3498.       XSTRING (dir)->data[0] = '~';
  3499.     }
  3500.  
  3501.   if (!NILP (initial))
  3502.     insdef = initial;
  3503.   else if (insert_default_directory)
  3504.     insdef = dir;
  3505.   else
  3506.     insdef = build_string ("");
  3507.  
  3508. #ifdef VMS
  3509.   count = specpdl_ptr - specpdl;
  3510.   specbind (intern ("completion-ignore-case"), Qt);
  3511. #endif
  3512.  
  3513.   GCPRO2 (insdef, defalt);
  3514.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  3515.               dir, mustmatch,
  3516.               insert_default_directory ? insdef : Qnil,
  3517.               Qfile_name_history);
  3518.  
  3519. #ifdef VMS
  3520.   unbind_to (count, Qnil);
  3521. #endif
  3522.  
  3523.   UNGCPRO;
  3524.   if (NILP (val))
  3525.     error ("No file name specified");
  3526.   tem = Fstring_equal (val, insdef);
  3527.   if (!NILP (tem) && !NILP (defalt))
  3528.     return defalt;
  3529.   return Fsubstitute_in_file_name (val);
  3530. }
  3531. #endif /* Old version */
  3532.  
  3533. _VOID_
  3534. syms_of_fileio ()
  3535. {
  3536.   Qexpand_file_name = intern ("expand-file-name");
  3537.   Qdirectory_file_name = intern ("directory-file-name");
  3538.   Qfile_name_directory = intern ("file-name-directory");
  3539.   Qfile_name_nondirectory = intern ("file-name-nondirectory");
  3540.   Qunhandled_file_name_directory = intern ("unhandled-file-name-directory");
  3541.   Qfile_name_as_directory = intern ("file-name-as-directory");
  3542.   Qcopy_file = intern ("copy-file");
  3543.   Qmake_directory = intern ("make-directory");
  3544.   Qdelete_directory = intern ("delete-directory");
  3545.   Qdelete_file = intern ("delete-file");
  3546.   Qrename_file = intern ("rename-file");
  3547.   Qadd_name_to_file = intern ("add-name-to-file");
  3548.   Qmake_symbolic_link = intern ("make-symbolic-link");
  3549.   Qfile_exists_p = intern ("file-exists-p");
  3550.   Qfile_executable_p = intern ("file-executable-p");
  3551.   Qfile_readable_p = intern ("file-readable-p");
  3552.   Qfile_symlink_p = intern ("file-symlink-p");
  3553.   Qfile_writable_p = intern ("file-writable-p");
  3554.   Qfile_directory_p = intern ("file-directory-p");
  3555.   Qfile_accessible_directory_p = intern ("file-accessible-directory-p");
  3556.   Qfile_modes = intern ("file-modes");
  3557.   Qset_file_modes = intern ("set-file-modes");
  3558.   Qfile_newer_than_file_p = intern ("file-newer-than-file-p");
  3559.   Qinsert_file_contents = intern ("insert-file-contents");
  3560.   Qwrite_region = intern ("write-region");
  3561.   Qverify_visited_file_modtime = intern ("verify-visited-file-modtime");
  3562.   Qset_visited_file_modtime = intern ("set-visited-file-modtime");
  3563.  
  3564.   staticpro (&Qexpand_file_name);
  3565.   staticpro (&Qdirectory_file_name);
  3566.   staticpro (&Qfile_name_directory);
  3567.   staticpro (&Qfile_name_nondirectory);
  3568.   staticpro (&Qunhandled_file_name_directory);
  3569.   staticpro (&Qfile_name_as_directory);
  3570.   staticpro (&Qcopy_file);
  3571.   staticpro (&Qmake_directory);
  3572.   staticpro (&Qdelete_directory);
  3573.   staticpro (&Qdelete_file);
  3574.   staticpro (&Qrename_file);
  3575.   staticpro (&Qadd_name_to_file);
  3576.   staticpro (&Qmake_symbolic_link);
  3577.   staticpro (&Qfile_exists_p);
  3578.   staticpro (&Qfile_executable_p);
  3579.   staticpro (&Qfile_readable_p);
  3580.   staticpro (&Qfile_symlink_p);
  3581.   staticpro (&Qfile_writable_p);
  3582.   staticpro (&Qfile_directory_p);
  3583.   staticpro (&Qfile_accessible_directory_p);
  3584.   staticpro (&Qfile_modes);
  3585.   staticpro (&Qset_file_modes);
  3586.   staticpro (&Qfile_newer_than_file_p);
  3587.   staticpro (&Qinsert_file_contents);
  3588.   staticpro (&Qwrite_region);
  3589.   staticpro (&Qverify_visited_file_modtime);
  3590.  
  3591.   Qfile_name_history = intern ("file-name-history");
  3592.   Fset (Qfile_name_history, Qnil);
  3593.   staticpro (&Qfile_name_history);
  3594.  
  3595.   Qfile_error = intern ("file-error");
  3596.   staticpro (&Qfile_error);
  3597.   Qfile_already_exists = intern("file-already-exists");
  3598.   staticpro (&Qfile_already_exists);
  3599.  
  3600.   Fput (Qfile_error, Qerror_conditions,
  3601.     Fcons (Qfile_error, Fcons (Qerror, Qnil)));
  3602.   Fput (Qfile_error, Qerror_message,
  3603.     build_string ("File error"));
  3604.  
  3605.   Fput (Qfile_already_exists, Qerror_conditions,
  3606.     Fcons (Qfile_already_exists,
  3607.            Fcons (Qfile_error, Fcons (Qerror, Qnil))));
  3608.   Fput (Qfile_already_exists, Qerror_message,
  3609.     build_string ("File already exists"));
  3610.  
  3611.   DEFVAR_BOOL ("insert-default-directory", &insert_default_directory,
  3612.     "*Non-nil means when reading a filename start with default dir in minibuffer.");
  3613.   insert_default_directory = 1;
  3614.  
  3615.   DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm,
  3616.     "*Non-nil means write new files with record format `stmlf'.\n\
  3617. nil means use format `var'.  This variable is meaningful only on VMS.");
  3618.   vms_stmlf_recfm = 0;
  3619.  
  3620.   DEFVAR_LISP ("file-name-handler-alist", &Vfile_name_handler_alist,
  3621.     "*Alist of elements (REGEXP . HANDLER) for file names handled specially.\n\
  3622. If a file name matches REGEXP, then all I/O on that file is done by calling\n\
  3623. HANDLER.\n\
  3624. \n\
  3625. The first argument given to HANDLER is the name of the I/O primitive\n\
  3626. to be handled; the remaining arguments are the arguments that were\n\
  3627. passed to that primitive.  For example, if you do\n\
  3628.     (file-exists-p FILENAME)\n\
  3629. and FILENAME is handled by HANDLER, then HANDLER is called like this:\n\
  3630.     (funcall HANDLER 'file-exists-p FILENAME)\n\
  3631. The function `find-file-name-handler' checks this list for a handler\n\
  3632. for its argument.");
  3633.   Vfile_name_handler_alist = Qnil;
  3634.  
  3635.   defsubr (&Sfind_file_name_handler);
  3636.   defsubr (&Sfile_name_directory);
  3637.   defsubr (&Sfile_name_nondirectory);
  3638.   defsubr (&Sunhandled_file_name_directory);
  3639.   defsubr (&Sfile_name_as_directory);
  3640.   defsubr (&Sdirectory_file_name);
  3641.   defsubr (&Smake_temp_name);
  3642.   defsubr (&Sexpand_file_name);
  3643.   defsubr (&Ssubstitute_in_file_name);
  3644.   defsubr (&Scopy_file);
  3645.   defsubr (&Smake_directory_internal);
  3646.   defsubr (&Sdelete_directory);
  3647.   defsubr (&Sdelete_file);
  3648.   defsubr (&Srename_file);
  3649.   defsubr (&Sadd_name_to_file);
  3650. #ifdef S_IFLNK
  3651.   defsubr (&Smake_symbolic_link);
  3652. #endif /* S_IFLNK */
  3653. #ifdef VMS
  3654.   defsubr (&Sdefine_logical_name);
  3655. #endif /* VMS */
  3656. #ifdef HPUX_NET
  3657.   defsubr (&Ssysnetunam);
  3658. #endif /* HPUX_NET */
  3659.   defsubr (&Sfile_name_absolute_p);
  3660.   defsubr (&Sfile_exists_p);
  3661.   defsubr (&Sfile_executable_p);
  3662.   defsubr (&Sfile_readable_p);
  3663.   defsubr (&Sfile_writable_p);
  3664.   defsubr (&Sfile_symlink_p);
  3665.   defsubr (&Sfile_directory_p);
  3666.   defsubr (&Sfile_accessible_directory_p);
  3667.   defsubr (&Sfile_modes);
  3668.   defsubr (&Sset_file_modes);
  3669.   defsubr (&Sset_default_file_modes);
  3670.   defsubr (&Sdefault_file_modes);
  3671.   defsubr (&Sfile_newer_than_file_p);
  3672.   defsubr (&Sinsert_file_contents);
  3673.   defsubr (&Swrite_region);
  3674.   defsubr (&Sverify_visited_file_modtime);
  3675.   defsubr (&Sclear_visited_file_modtime);
  3676.   defsubr (&Svisited_file_modtime);
  3677.   defsubr (&Sset_visited_file_modtime);
  3678.   defsubr (&Sdo_auto_save);
  3679.   defsubr (&Sset_buffer_auto_saved);
  3680.   defsubr (&Srecent_auto_save_p);
  3681.  
  3682.   defsubr (&Sread_file_name_internal);
  3683.   defsubr (&Sread_file_name);
  3684.  
  3685. #ifdef unix
  3686.   defsubr (&Sunix_sync);
  3687. #endif
  3688. }
  3689.